Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Overview: |
| 3 | * This is the generic MTD driver for NAND flash devices. It should be |
| 4 | * capable of working with almost all NAND chips currently available. |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Additional technical information is available on |
maximilian attems | 8b2b403 | 2007-07-28 13:07:16 +0200 | [diff] [blame] | 7 | * http://www.linux-mtd.infradead.org/doc/nand.html |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 10 | * 2002-2006 Thomas Gleixner (tglx@linutronix.de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 12 | * Credits: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 13 | * David Woodhouse for adding multichip support |
| 14 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * Aleph One Ltd. and Toby Churchill Ltd. for supporting the |
| 16 | * rework for 2K page size chips |
| 17 | * |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 18 | * TODO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * Enable cached programming for 2k page size chips |
| 20 | * Check, if mtd->ecctype should be set to MTD_ECC_HW |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 21 | * if we have HW ECC support. |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 22 | * BBT table is not serialized, has to be fixed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * This program is free software; you can redistribute it and/or modify |
| 25 | * it under the terms of the GNU General Public License version 2 as |
| 26 | * published by the Free Software Foundation. |
| 27 | * |
| 28 | */ |
| 29 | |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 30 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 31 | |
David Woodhouse | 552d920 | 2006-05-14 01:20:46 +0100 | [diff] [blame] | 32 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/delay.h> |
| 34 | #include <linux/errno.h> |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 35 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/sched.h> |
| 37 | #include <linux/slab.h> |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 38 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/types.h> |
| 40 | #include <linux/mtd/mtd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/mtd/nand_ecc.h> |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 42 | #include <linux/mtd/nand_bch.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/interrupt.h> |
| 44 | #include <linux/bitops.h> |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 45 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <linux/mtd/partitions.h> |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 47 | #include <linux/of.h> |
Janusz Krzysztofik | b0e137a | 2018-10-15 21:41:28 +0200 | [diff] [blame] | 48 | #include <linux/gpio/consumer.h> |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 49 | |
Boris Brezillon | 348d56a | 2018-09-07 00:38:48 +0200 | [diff] [blame] | 50 | #include "internals.h" |
| 51 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 52 | static int nand_get_device(struct nand_chip *chip, int new_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 54 | static int nand_do_write_oob(struct nand_chip *chip, loff_t to, |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 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 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 217 | static int check_offs_len(struct nand_chip *chip, loff_t ofs, uint64_t len) |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 218 | { |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 219 | int ret = 0; |
| 220 | |
| 221 | /* Start address must align on block boundary */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 222 | if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 223 | pr_debug("%s: unaligned address\n", __func__); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 224 | ret = -EINVAL; |
| 225 | } |
| 226 | |
| 227 | /* Length must align on block boundary */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 228 | if (len & ((1ULL << chip->phys_erase_shift) - 1)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 229 | pr_debug("%s: length not block aligned\n", __func__); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 230 | ret = -EINVAL; |
| 231 | } |
| 232 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 233 | return ret; |
| 234 | } |
| 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | /** |
| 237 | * nand_release_device - [GENERIC] release chip |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 238 | * @chip: NAND chip object |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 239 | * |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 240 | * Release chip lock and wake up anyone waiting on the device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 242 | static void nand_release_device(struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | { |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 244 | /* Release the controller and the chip */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 245 | spin_lock(&chip->controller->lock); |
| 246 | chip->controller->active = NULL; |
| 247 | chip->state = FL_READY; |
| 248 | wake_up(&chip->controller->wq); |
| 249 | spin_unlock(&chip->controller->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 254 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 255 | * @ofs: offset from device start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 257 | * Check, if the block is bad. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | */ |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 259 | static int nand_block_bad(struct nand_chip *chip, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 261 | struct mtd_info *mtd = nand_to_mtd(chip); |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 262 | int page, page_end, res; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 263 | u8 bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
Brian Norris | 5fb1549 | 2011-05-31 16:31:21 -0700 | [diff] [blame] | 265 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
Kevin Cernekee | b60b08b | 2010-05-04 20:58:10 -0700 | [diff] [blame] | 266 | ofs += mtd->erasesize - mtd->writesize; |
| 267 | |
Thomas Knobloch | 1a12f46 | 2007-05-03 07:39:37 +0100 | [diff] [blame] | 268 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 269 | page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1); |
Thomas Knobloch | 1a12f46 | 2007-05-03 07:39:37 +0100 | [diff] [blame] | 270 | |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 271 | for (; page < page_end; page++) { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 272 | res = chip->ecc.read_oob(chip, page); |
Abhishek Sahu | e9893e6 | 2018-06-13 14:32:36 +0530 | [diff] [blame] | 273 | if (res < 0) |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 274 | return res; |
| 275 | |
| 276 | bad = chip->oob_poi[chip->badblockpos]; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 277 | |
Brian Norris | cdbec05 | 2012-01-13 18:11:48 -0800 | [diff] [blame] | 278 | if (likely(chip->badblockbits == 8)) |
| 279 | res = bad != 0xFF; |
| 280 | else |
| 281 | res = hweight8(bad) < chip->badblockbits; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 282 | if (res) |
| 283 | return res; |
| 284 | } |
Maxim Levitsky | e0b58d0 | 2010-02-22 20:39:38 +0200 | [diff] [blame] | 285 | |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 286 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /** |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 290 | * 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] | 291 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 292 | * @ofs: offset from device start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 294 | * This is the default implementation, which can be overridden by a hardware |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 295 | * specific driver. It provides the details for writing a bad block marker to a |
| 296 | * block. |
| 297 | */ |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 298 | 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] | 299 | { |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 300 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 301 | struct mtd_oob_ops ops; |
| 302 | uint8_t buf[2] = { 0, 0 }; |
| 303 | int ret = 0, res, i = 0; |
| 304 | |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 305 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 306 | ops.oobbuf = buf; |
| 307 | ops.ooboffs = chip->badblockpos; |
| 308 | if (chip->options & NAND_BUSWIDTH_16) { |
| 309 | ops.ooboffs &= ~0x01; |
| 310 | ops.len = ops.ooblen = 2; |
| 311 | } else { |
| 312 | ops.len = ops.ooblen = 1; |
| 313 | } |
| 314 | ops.mode = MTD_OPS_PLACE_OOB; |
| 315 | |
| 316 | /* Write to first/last page(s) if necessary */ |
| 317 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
| 318 | ofs += mtd->erasesize - mtd->writesize; |
| 319 | do { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 320 | res = nand_do_write_oob(chip, ofs, &ops); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 321 | if (!ret) |
| 322 | ret = res; |
| 323 | |
| 324 | i++; |
| 325 | ofs += mtd->writesize; |
| 326 | } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2); |
| 327 | |
| 328 | return ret; |
| 329 | } |
| 330 | |
| 331 | /** |
Boris Brezillon | cdc784c | 2018-09-07 00:38:38 +0200 | [diff] [blame] | 332 | * nand_markbad_bbm - mark a block by updating the BBM |
| 333 | * @chip: NAND chip object |
| 334 | * @ofs: offset of the block to mark bad |
| 335 | */ |
| 336 | int nand_markbad_bbm(struct nand_chip *chip, loff_t ofs) |
| 337 | { |
| 338 | if (chip->legacy.block_markbad) |
| 339 | return chip->legacy.block_markbad(chip, ofs); |
| 340 | |
| 341 | return nand_default_block_markbad(chip, ofs); |
| 342 | } |
| 343 | |
| 344 | static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs) |
| 345 | { |
| 346 | if (chip->legacy.block_bad) |
| 347 | return chip->legacy.block_bad(chip, ofs); |
| 348 | |
| 349 | return nand_block_bad(chip, ofs); |
| 350 | } |
| 351 | |
| 352 | /** |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 353 | * nand_block_markbad_lowlevel - mark a block bad |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 354 | * @chip: NAND chip object |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 355 | * @ofs: offset from device start |
| 356 | * |
| 357 | * This function performs the generic NAND bad block marking steps (i.e., bad |
| 358 | * 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] | 359 | * 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] | 360 | * |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 361 | * We try operations in the following order: |
Mauro Carvalho Chehab | b6f6c29 | 2017-05-13 07:40:36 -0300 | [diff] [blame] | 362 | * |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 363 | * (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] | 364 | * (2) write bad block marker to OOB area of affected block (unless flag |
| 365 | * NAND_BBT_NO_OOB_BBM is present) |
| 366 | * (3) update the BBT |
Mauro Carvalho Chehab | b6f6c29 | 2017-05-13 07:40:36 -0300 | [diff] [blame] | 367 | * |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 368 | * 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] | 369 | * procedures, and dump the error in the end. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 371 | static int nand_block_markbad_lowlevel(struct nand_chip *chip, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 373 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 374 | int res, ret = 0; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 375 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 376 | if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) { |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 377 | struct erase_info einfo; |
| 378 | |
| 379 | /* Attempt erase before marking OOB */ |
| 380 | memset(&einfo, 0, sizeof(einfo)); |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 381 | einfo.addr = ofs; |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 382 | einfo.len = 1ULL << chip->phys_erase_shift; |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 383 | nand_erase_nand(chip, &einfo, 0); |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 384 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 385 | /* Write bad block marker to OOB */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 386 | nand_get_device(chip, FL_WRITING); |
Boris Brezillon | cdc784c | 2018-09-07 00:38:38 +0200 | [diff] [blame] | 387 | ret = nand_markbad_bbm(chip, ofs); |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 388 | nand_release_device(chip); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 389 | } |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 390 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 391 | /* Mark block bad in BBT */ |
| 392 | if (chip->bbt) { |
Boris Brezillon | 5740d4c | 2018-09-06 14:05:34 +0200 | [diff] [blame] | 393 | res = nand_markbad_bbt(chip, ofs); |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 394 | if (!ret) |
| 395 | ret = res; |
| 396 | } |
| 397 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 398 | if (!ret) |
| 399 | mtd->ecc_stats.badblocks++; |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 400 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 401 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 404 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | * nand_check_wp - [GENERIC] check if the chip is write protected |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 406 | * @chip: NAND chip object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 408 | * Check, if the device is write protected. The function expects, that the |
| 409 | * device is already selected. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 411 | static int nand_check_wp(struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 413 | u8 status; |
| 414 | int ret; |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 415 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 416 | /* Broken xD cards report WP despite being writable */ |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 417 | if (chip->options & NAND_BROKEN_XD) |
| 418 | return 0; |
| 419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | /* Check the WP bit */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 421 | ret = nand_status_op(chip, &status); |
| 422 | if (ret) |
| 423 | return ret; |
| 424 | |
| 425 | return status & NAND_STATUS_WP ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /** |
Gu Zheng | c30e1f7 | 2014-09-03 17:49:10 +0800 | [diff] [blame] | 429 | * nand_block_isreserved - [GENERIC] Check if a block is marked reserved. |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 430 | * @mtd: MTD device structure |
| 431 | * @ofs: offset from device start |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 432 | * |
Gu Zheng | c30e1f7 | 2014-09-03 17:49:10 +0800 | [diff] [blame] | 433 | * Check if the block is marked as reserved. |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 434 | */ |
| 435 | static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
| 436 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 437 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 438 | |
| 439 | if (!chip->bbt) |
| 440 | return 0; |
| 441 | /* Return info from the table */ |
Boris Brezillon | 5740d4c | 2018-09-06 14:05:34 +0200 | [diff] [blame] | 442 | return nand_isreserved_bbt(chip, ofs); |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /** |
| 446 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 447 | * @chip: NAND chip object |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 448 | * @ofs: offset from device start |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 449 | * @allowbbt: 1, if its allowed to access the bbt area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | * |
| 451 | * Check, if the block is bad. Either by reading the bad block table or |
| 452 | * calling of the scan function. |
| 453 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 454 | static int nand_block_checkbad(struct nand_chip *chip, loff_t ofs, int allowbbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | /* Return info from the table */ |
Boris Brezillon | cdc784c | 2018-09-07 00:38:38 +0200 | [diff] [blame] | 457 | if (chip->bbt) |
| 458 | return nand_isbad_bbt(chip, ofs, allowbbt); |
| 459 | |
| 460 | return nand_isbad_bbm(chip, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 463 | /** |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 464 | * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1 |
| 465 | * @chip: NAND chip structure |
| 466 | * @timeout_ms: Timeout in ms |
| 467 | * |
| 468 | * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1. |
| 469 | * If that does not happen whitin the specified timeout, -ETIMEDOUT is |
| 470 | * returned. |
| 471 | * |
| 472 | * This helper is intended to be used when the controller does not have access |
| 473 | * to the NAND R/B pin. |
| 474 | * |
| 475 | * Be aware that calling this helper from an ->exec_op() implementation means |
| 476 | * ->exec_op() must be re-entrant. |
| 477 | * |
| 478 | * Return 0 if the NAND chip is ready, a negative error otherwise. |
| 479 | */ |
| 480 | int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms) |
| 481 | { |
Boris Brezillon | 3057fce | 2018-05-04 21:24:31 +0200 | [diff] [blame] | 482 | const struct nand_sdr_timings *timings; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 483 | u8 status = 0; |
| 484 | int ret; |
| 485 | |
| 486 | if (!chip->exec_op) |
| 487 | return -ENOTSUPP; |
| 488 | |
Boris Brezillon | 3057fce | 2018-05-04 21:24:31 +0200 | [diff] [blame] | 489 | /* Wait tWB before polling the STATUS reg. */ |
| 490 | timings = nand_get_sdr_timings(&chip->data_interface); |
| 491 | ndelay(PSEC_TO_NSEC(timings->tWB_max)); |
| 492 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 493 | ret = nand_status_op(chip, NULL); |
| 494 | if (ret) |
| 495 | return ret; |
| 496 | |
| 497 | timeout_ms = jiffies + msecs_to_jiffies(timeout_ms); |
| 498 | do { |
| 499 | ret = nand_read_data_op(chip, &status, sizeof(status), true); |
| 500 | if (ret) |
| 501 | break; |
| 502 | |
| 503 | if (status & NAND_STATUS_READY) |
| 504 | break; |
| 505 | |
| 506 | /* |
| 507 | * Typical lowest execution time for a tR on most NANDs is 10us, |
| 508 | * use this as polling delay before doing something smarter (ie. |
| 509 | * deriving a delay from the timeout value, timeout_ms/ratio). |
| 510 | */ |
| 511 | udelay(10); |
| 512 | } while (time_before(jiffies, timeout_ms)); |
| 513 | |
| 514 | /* |
| 515 | * We have to exit READ_STATUS mode in order to read real data on the |
| 516 | * bus in case the WAITRDY instruction is preceding a DATA_IN |
| 517 | * instruction. |
| 518 | */ |
| 519 | nand_exit_status_op(chip); |
| 520 | |
| 521 | if (ret) |
| 522 | return ret; |
| 523 | |
| 524 | return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT; |
| 525 | }; |
| 526 | EXPORT_SYMBOL_GPL(nand_soft_waitrdy); |
| 527 | |
| 528 | /** |
Janusz Krzysztofik | b0e137a | 2018-10-15 21:41:28 +0200 | [diff] [blame] | 529 | * nand_gpio_waitrdy - Poll R/B GPIO pin until ready |
| 530 | * @chip: NAND chip structure |
| 531 | * @gpiod: GPIO descriptor of R/B pin |
| 532 | * @timeout_ms: Timeout in ms |
| 533 | * |
| 534 | * Poll the R/B GPIO pin until it becomes ready. If that does not happen |
| 535 | * whitin the specified timeout, -ETIMEDOUT is returned. |
| 536 | * |
| 537 | * This helper is intended to be used when the controller has access to the |
| 538 | * NAND R/B pin over GPIO. |
| 539 | * |
| 540 | * Return 0 if the R/B pin indicates chip is ready, a negative error otherwise. |
| 541 | */ |
| 542 | int nand_gpio_waitrdy(struct nand_chip *chip, struct gpio_desc *gpiod, |
| 543 | unsigned long timeout_ms) |
| 544 | { |
| 545 | /* Wait until R/B pin indicates chip is ready or timeout occurs */ |
| 546 | timeout_ms = jiffies + msecs_to_jiffies(timeout_ms); |
| 547 | do { |
| 548 | if (gpiod_get_value_cansleep(gpiod)) |
| 549 | return 0; |
| 550 | |
| 551 | cond_resched(); |
| 552 | } while (time_before(jiffies, timeout_ms)); |
| 553 | |
| 554 | return gpiod_get_value_cansleep(gpiod) ? 0 : -ETIMEDOUT; |
| 555 | }; |
| 556 | EXPORT_SYMBOL_GPL(nand_gpio_waitrdy); |
| 557 | |
| 558 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 559 | * panic_nand_get_device - [GENERIC] Get chip for selected access |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 560 | * @chip: the nand chip descriptor |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 561 | * @new_state: the state which is requested |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 562 | * |
| 563 | * Used when in panic, no locks are taken. |
| 564 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 565 | static void panic_nand_get_device(struct nand_chip *chip, int new_state) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 566 | { |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 567 | /* Hardware controller shared among independent devices */ |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 568 | chip->controller->active = chip; |
| 569 | chip->state = new_state; |
| 570 | } |
| 571 | |
| 572 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | * nand_get_device - [GENERIC] Get chip for selected access |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 574 | * @chip: NAND chip structure |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 575 | * @new_state: the state which is requested |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | * |
| 577 | * Get the device and lock it for exclusive access |
| 578 | */ |
Thomas Gleixner | 2c0a2be | 2006-05-23 11:50:56 +0200 | [diff] [blame] | 579 | static int |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 580 | nand_get_device(struct nand_chip *chip, int new_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 582 | spinlock_t *lock = &chip->controller->lock; |
| 583 | wait_queue_head_t *wq = &chip->controller->wq; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 584 | DECLARE_WAITQUEUE(wait, current); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 585 | retry: |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 586 | spin_lock(lock); |
| 587 | |
vimal singh | b8b3ee9 | 2009-07-09 20:41:22 +0530 | [diff] [blame] | 588 | /* Hardware controller shared among independent devices */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 589 | if (!chip->controller->active) |
| 590 | chip->controller->active = chip; |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 591 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 592 | if (chip->controller->active == chip && chip->state == FL_READY) { |
| 593 | chip->state = new_state; |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 594 | spin_unlock(lock); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 595 | return 0; |
| 596 | } |
| 597 | if (new_state == FL_PM_SUSPENDED) { |
Li Yang | 6b0d9a8 | 2009-11-17 14:45:49 -0800 | [diff] [blame] | 598 | if (chip->controller->active->state == FL_PM_SUSPENDED) { |
| 599 | chip->state = FL_PM_SUSPENDED; |
| 600 | spin_unlock(lock); |
| 601 | return 0; |
Li Yang | 6b0d9a8 | 2009-11-17 14:45:49 -0800 | [diff] [blame] | 602 | } |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 603 | } |
| 604 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 605 | add_wait_queue(wq, &wait); |
| 606 | spin_unlock(lock); |
| 607 | schedule(); |
| 608 | remove_wait_queue(wq, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | goto retry; |
| 610 | } |
| 611 | |
| 612 | /** |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 613 | * panic_nand_wait - [GENERIC] wait until the command is done |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 614 | * @chip: NAND chip structure |
| 615 | * @timeo: timeout |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 616 | * |
| 617 | * Wait for command done. This is a helper function for nand_wait used when |
| 618 | * 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] | 619 | * an oops through mtdoops. |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 620 | */ |
Boris Brezillon | 3d4af7c | 2018-09-07 00:38:49 +0200 | [diff] [blame] | 621 | void panic_nand_wait(struct nand_chip *chip, unsigned long timeo) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 622 | { |
| 623 | int i; |
| 624 | for (i = 0; i < timeo; i++) { |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 625 | if (chip->legacy.dev_ready) { |
| 626 | if (chip->legacy.dev_ready(chip)) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 627 | break; |
| 628 | } else { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 629 | int ret; |
| 630 | u8 status; |
| 631 | |
| 632 | ret = nand_read_data_op(chip, &status, sizeof(status), |
| 633 | true); |
| 634 | if (ret) |
| 635 | return; |
| 636 | |
| 637 | if (status & NAND_STATUS_READY) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 638 | break; |
| 639 | } |
| 640 | mdelay(1); |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 641 | } |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 642 | } |
| 643 | |
Miquel Raynal | 789157e | 2018-03-19 14:47:28 +0100 | [diff] [blame] | 644 | static bool nand_supports_get_features(struct nand_chip *chip, int addr) |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 645 | { |
Miquel Raynal | 789157e | 2018-03-19 14:47:28 +0100 | [diff] [blame] | 646 | return (chip->parameters.supports_set_get_features && |
| 647 | test_bit(addr, chip->parameters.get_feature_list)); |
| 648 | } |
| 649 | |
| 650 | static bool nand_supports_set_features(struct nand_chip *chip, int addr) |
| 651 | { |
| 652 | return (chip->parameters.supports_set_get_features && |
| 653 | test_bit(addr, chip->parameters.set_feature_list)); |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | /** |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 657 | * nand_reset_data_interface - Reset data interface and timings |
| 658 | * @chip: The NAND chip |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 659 | * @chipnr: Internal die id |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 660 | * |
| 661 | * Reset the Data interface and timings to ONFI mode 0. |
| 662 | * |
| 663 | * Returns 0 for success or negative error code otherwise. |
| 664 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 665 | static int nand_reset_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 666 | { |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 667 | int ret; |
| 668 | |
| 669 | if (!chip->setup_data_interface) |
| 670 | return 0; |
| 671 | |
| 672 | /* |
| 673 | * The ONFI specification says: |
| 674 | * " |
| 675 | * To transition from NV-DDR or NV-DDR2 to the SDR data |
| 676 | * interface, the host shall use the Reset (FFh) command |
| 677 | * using SDR timing mode 0. A device in any timing mode is |
| 678 | * required to recognize Reset (FFh) command issued in SDR |
| 679 | * timing mode 0. |
| 680 | * " |
| 681 | * |
| 682 | * Configure the data interface in SDR mode and set the |
| 683 | * timings to timing mode 0. |
| 684 | */ |
| 685 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 686 | onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0); |
Boris Brezillon | 858838b | 2018-09-06 14:05:33 +0200 | [diff] [blame] | 687 | ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 688 | if (ret) |
| 689 | pr_err("Failed to configure data interface to SDR timing mode 0\n"); |
| 690 | |
| 691 | return ret; |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * nand_setup_data_interface - Setup the best data interface and timings |
| 696 | * @chip: The NAND chip |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 697 | * @chipnr: Internal die id |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 698 | * |
| 699 | * Find and configure the best data interface and NAND timings supported by |
| 700 | * the chip and the driver. |
| 701 | * First tries to retrieve supported timing modes from ONFI information, |
| 702 | * and if the NAND chip does not support ONFI, relies on the |
| 703 | * ->onfi_timing_mode_default specified in the nand_ids table. |
| 704 | * |
| 705 | * Returns 0 for success or negative error code otherwise. |
| 706 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 707 | static int nand_setup_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 708 | { |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 709 | u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = { |
| 710 | chip->onfi_timing_mode_default, |
| 711 | }; |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 712 | int ret; |
| 713 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 714 | if (!chip->setup_data_interface) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 715 | return 0; |
| 716 | |
Miquel Raynal | 993447b | 2018-03-19 14:47:21 +0100 | [diff] [blame] | 717 | /* 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] | 718 | if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) { |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 719 | chip->select_chip(chip, chipnr); |
Miquel Raynal | 993447b | 2018-03-19 14:47:21 +0100 | [diff] [blame] | 720 | ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE, |
| 721 | tmode_param); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 722 | chip->select_chip(chip, -1); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 723 | if (ret) |
Miquel Raynal | 993447b | 2018-03-19 14:47:21 +0100 | [diff] [blame] | 724 | return ret; |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 725 | } |
| 726 | |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 727 | /* Change the mode on the controller side */ |
Boris Brezillon | 858838b | 2018-09-06 14:05:33 +0200 | [diff] [blame] | 728 | ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 729 | if (ret) |
| 730 | return ret; |
| 731 | |
| 732 | /* Check the mode has been accepted by the chip, if supported */ |
Miquel Raynal | 789157e | 2018-03-19 14:47:28 +0100 | [diff] [blame] | 733 | if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 734 | return 0; |
| 735 | |
| 736 | memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 737 | chip->select_chip(chip, chipnr); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 738 | ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE, |
| 739 | tmode_param); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 740 | chip->select_chip(chip, -1); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 741 | if (ret) |
| 742 | goto err_reset_chip; |
| 743 | |
| 744 | if (tmode_param[0] != chip->onfi_timing_mode_default) { |
| 745 | pr_warn("timing mode %d not acknowledged by the NAND chip\n", |
| 746 | chip->onfi_timing_mode_default); |
| 747 | goto err_reset_chip; |
| 748 | } |
| 749 | |
| 750 | return 0; |
| 751 | |
| 752 | err_reset_chip: |
| 753 | /* |
| 754 | * Fallback to mode 0 if the chip explicitly did not ack the chosen |
| 755 | * timing mode. |
| 756 | */ |
| 757 | nand_reset_data_interface(chip, chipnr); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 758 | chip->select_chip(chip, chipnr); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 759 | nand_reset_op(chip); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 760 | chip->select_chip(chip, -1); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 761 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 762 | return ret; |
| 763 | } |
| 764 | |
| 765 | /** |
| 766 | * nand_init_data_interface - find the best data interface and timings |
| 767 | * @chip: The NAND chip |
| 768 | * |
| 769 | * Find the best data interface and NAND timings supported by the chip |
| 770 | * and the driver. |
| 771 | * First tries to retrieve supported timing modes from ONFI information, |
| 772 | * and if the NAND chip does not support ONFI, relies on the |
| 773 | * ->onfi_timing_mode_default specified in the nand_ids table. After this |
| 774 | * function nand_chip->data_interface is initialized with the best timing mode |
| 775 | * available. |
| 776 | * |
| 777 | * Returns 0 for success or negative error code otherwise. |
| 778 | */ |
| 779 | static int nand_init_data_interface(struct nand_chip *chip) |
| 780 | { |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 781 | int modes, mode, ret; |
| 782 | |
| 783 | if (!chip->setup_data_interface) |
| 784 | return 0; |
| 785 | |
| 786 | /* |
| 787 | * First try to identify the best timings from ONFI parameters and |
| 788 | * if the NAND does not support ONFI, fallback to the default ONFI |
| 789 | * timing mode. |
| 790 | */ |
Boris Brezillon | 462f35d | 2018-09-07 00:38:47 +0200 | [diff] [blame] | 791 | if (chip->parameters.onfi) { |
| 792 | modes = chip->parameters.onfi->async_timing_mode; |
| 793 | } else { |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 794 | if (!chip->onfi_timing_mode_default) |
| 795 | return 0; |
| 796 | |
| 797 | modes = GENMASK(chip->onfi_timing_mode_default, 0); |
| 798 | } |
| 799 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 800 | for (mode = fls(modes) - 1; mode >= 0; mode--) { |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 801 | ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 802 | if (ret) |
| 803 | continue; |
| 804 | |
Miquel Raynal | d787b8b | 2017-12-22 18:12:41 +0100 | [diff] [blame] | 805 | /* |
| 806 | * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the |
| 807 | * controller supports the requested timings. |
| 808 | */ |
Boris Brezillon | 858838b | 2018-09-06 14:05:33 +0200 | [diff] [blame] | 809 | ret = chip->setup_data_interface(chip, |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 810 | NAND_DATA_IFACE_CHECK_ONLY, |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 811 | &chip->data_interface); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 812 | if (!ret) { |
| 813 | chip->onfi_timing_mode_default = mode; |
| 814 | break; |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | return 0; |
| 819 | } |
| 820 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 821 | /** |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 822 | * nand_fill_column_cycles - fill the column cycles of an address |
| 823 | * @chip: The NAND chip |
| 824 | * @addrs: Array of address cycles to fill |
| 825 | * @offset_in_page: The offset in the page |
| 826 | * |
| 827 | * Fills the first or the first two bytes of the @addrs field depending |
| 828 | * on the NAND bus width and the page size. |
| 829 | * |
| 830 | * Returns the number of cycles needed to encode the column, or a negative |
| 831 | * error code in case one of the arguments is invalid. |
| 832 | */ |
| 833 | static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs, |
| 834 | unsigned int offset_in_page) |
Matthieu CASTET | f251b8d | 2012-11-05 15:00:44 +0100 | [diff] [blame] | 835 | { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 836 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 837 | |
| 838 | /* Make sure the offset is less than the actual page size. */ |
| 839 | if (offset_in_page > mtd->writesize + mtd->oobsize) |
| 840 | return -EINVAL; |
| 841 | |
| 842 | /* |
| 843 | * On small page NANDs, there's a dedicated command to access the OOB |
| 844 | * area, and the column address is relative to the start of the OOB |
| 845 | * area, not the start of the page. Asjust the address accordingly. |
| 846 | */ |
| 847 | if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize) |
| 848 | offset_in_page -= mtd->writesize; |
| 849 | |
| 850 | /* |
| 851 | * The offset in page is expressed in bytes, if the NAND bus is 16-bit |
| 852 | * wide, then it must be divided by 2. |
| 853 | */ |
| 854 | if (chip->options & NAND_BUSWIDTH_16) { |
| 855 | if (WARN_ON(offset_in_page % 2)) |
| 856 | return -EINVAL; |
| 857 | |
| 858 | offset_in_page /= 2; |
| 859 | } |
| 860 | |
| 861 | addrs[0] = offset_in_page; |
| 862 | |
| 863 | /* |
| 864 | * Small page NANDs use 1 cycle for the columns, while large page NANDs |
| 865 | * need 2 |
| 866 | */ |
| 867 | if (mtd->writesize <= 512) |
| 868 | return 1; |
| 869 | |
| 870 | addrs[1] = offset_in_page >> 8; |
| 871 | |
| 872 | return 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | } |
| 874 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 875 | static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page, |
| 876 | unsigned int offset_in_page, void *buf, |
| 877 | unsigned int len) |
| 878 | { |
| 879 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 880 | const struct nand_sdr_timings *sdr = |
| 881 | nand_get_sdr_timings(&chip->data_interface); |
| 882 | u8 addrs[4]; |
| 883 | struct nand_op_instr instrs[] = { |
| 884 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 885 | NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)), |
| 886 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max), |
| 887 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 888 | NAND_OP_DATA_IN(len, buf, 0), |
| 889 | }; |
| 890 | struct nand_operation op = NAND_OPERATION(instrs); |
| 891 | int ret; |
| 892 | |
| 893 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 894 | if (!len) |
| 895 | op.ninstrs--; |
| 896 | |
| 897 | if (offset_in_page >= mtd->writesize) |
| 898 | instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB; |
| 899 | else if (offset_in_page >= 256 && |
| 900 | !(chip->options & NAND_BUSWIDTH_16)) |
| 901 | instrs[0].ctx.cmd.opcode = NAND_CMD_READ1; |
| 902 | |
| 903 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 904 | if (ret < 0) |
| 905 | return ret; |
| 906 | |
| 907 | addrs[1] = page; |
| 908 | addrs[2] = page >> 8; |
| 909 | |
| 910 | if (chip->options & NAND_ROW_ADDR_3) { |
| 911 | addrs[3] = page >> 16; |
| 912 | instrs[1].ctx.addr.naddrs++; |
| 913 | } |
| 914 | |
| 915 | return nand_exec_op(chip, &op); |
| 916 | } |
| 917 | |
| 918 | static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page, |
| 919 | unsigned int offset_in_page, void *buf, |
| 920 | unsigned int len) |
| 921 | { |
| 922 | const struct nand_sdr_timings *sdr = |
| 923 | nand_get_sdr_timings(&chip->data_interface); |
| 924 | u8 addrs[5]; |
| 925 | struct nand_op_instr instrs[] = { |
| 926 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 927 | NAND_OP_ADDR(4, addrs, 0), |
| 928 | NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)), |
| 929 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max), |
| 930 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 931 | NAND_OP_DATA_IN(len, buf, 0), |
| 932 | }; |
| 933 | struct nand_operation op = NAND_OPERATION(instrs); |
| 934 | int ret; |
| 935 | |
| 936 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 937 | if (!len) |
| 938 | op.ninstrs--; |
| 939 | |
| 940 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 941 | if (ret < 0) |
| 942 | return ret; |
| 943 | |
| 944 | addrs[2] = page; |
| 945 | addrs[3] = page >> 8; |
| 946 | |
| 947 | if (chip->options & NAND_ROW_ADDR_3) { |
| 948 | addrs[4] = page >> 16; |
| 949 | instrs[1].ctx.addr.naddrs++; |
| 950 | } |
| 951 | |
| 952 | return nand_exec_op(chip, &op); |
| 953 | } |
| 954 | |
| 955 | /** |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 956 | * nand_read_page_op - Do a READ PAGE operation |
| 957 | * @chip: The NAND chip |
| 958 | * @page: page to read |
| 959 | * @offset_in_page: offset within the page |
| 960 | * @buf: buffer used to store the data |
| 961 | * @len: length of the buffer |
| 962 | * |
| 963 | * This function issues a READ PAGE operation. |
| 964 | * This function does not select/unselect the CS line. |
| 965 | * |
| 966 | * Returns 0 on success, a negative error code otherwise. |
| 967 | */ |
| 968 | int nand_read_page_op(struct nand_chip *chip, unsigned int page, |
| 969 | unsigned int offset_in_page, void *buf, unsigned int len) |
| 970 | { |
| 971 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 972 | |
| 973 | if (len && !buf) |
| 974 | return -EINVAL; |
| 975 | |
| 976 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 977 | return -EINVAL; |
| 978 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 979 | if (chip->exec_op) { |
| 980 | if (mtd->writesize > 512) |
| 981 | return nand_lp_exec_read_page_op(chip, page, |
| 982 | offset_in_page, buf, |
| 983 | len); |
| 984 | |
| 985 | return nand_sp_exec_read_page_op(chip, page, offset_in_page, |
| 986 | buf, len); |
| 987 | } |
| 988 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 989 | chip->legacy.cmdfunc(chip, NAND_CMD_READ0, offset_in_page, page); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 990 | if (len) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 991 | chip->legacy.read_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 992 | |
| 993 | return 0; |
| 994 | } |
| 995 | EXPORT_SYMBOL_GPL(nand_read_page_op); |
| 996 | |
| 997 | /** |
| 998 | * nand_read_param_page_op - Do a READ PARAMETER PAGE operation |
| 999 | * @chip: The NAND chip |
| 1000 | * @page: parameter page to read |
| 1001 | * @buf: buffer used to store the data |
| 1002 | * @len: length of the buffer |
| 1003 | * |
| 1004 | * This function issues a READ PARAMETER PAGE operation. |
| 1005 | * This function does not select/unselect the CS line. |
| 1006 | * |
| 1007 | * Returns 0 on success, a negative error code otherwise. |
| 1008 | */ |
Boris Brezillon | 1c325cc | 2018-09-07 00:38:50 +0200 | [diff] [blame] | 1009 | int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf, |
| 1010 | unsigned int len) |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1011 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1012 | unsigned int i; |
| 1013 | u8 *p = buf; |
| 1014 | |
| 1015 | if (len && !buf) |
| 1016 | return -EINVAL; |
| 1017 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1018 | if (chip->exec_op) { |
| 1019 | const struct nand_sdr_timings *sdr = |
| 1020 | nand_get_sdr_timings(&chip->data_interface); |
| 1021 | struct nand_op_instr instrs[] = { |
| 1022 | NAND_OP_CMD(NAND_CMD_PARAM, 0), |
| 1023 | NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1024 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max), |
| 1025 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 1026 | NAND_OP_8BIT_DATA_IN(len, buf, 0), |
| 1027 | }; |
| 1028 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1029 | |
| 1030 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1031 | if (!len) |
| 1032 | op.ninstrs--; |
| 1033 | |
| 1034 | return nand_exec_op(chip, &op); |
| 1035 | } |
| 1036 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1037 | chip->legacy.cmdfunc(chip, NAND_CMD_PARAM, page, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1038 | for (i = 0; i < len; i++) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1039 | p[i] = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1040 | |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
| 1044 | /** |
| 1045 | * nand_change_read_column_op - Do a CHANGE READ COLUMN operation |
| 1046 | * @chip: The NAND chip |
| 1047 | * @offset_in_page: offset within the page |
| 1048 | * @buf: buffer used to store the data |
| 1049 | * @len: length of the buffer |
| 1050 | * @force_8bit: force 8-bit bus access |
| 1051 | * |
| 1052 | * This function issues a CHANGE READ COLUMN operation. |
| 1053 | * This function does not select/unselect the CS line. |
| 1054 | * |
| 1055 | * Returns 0 on success, a negative error code otherwise. |
| 1056 | */ |
| 1057 | int nand_change_read_column_op(struct nand_chip *chip, |
| 1058 | unsigned int offset_in_page, void *buf, |
| 1059 | unsigned int len, bool force_8bit) |
| 1060 | { |
| 1061 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1062 | |
| 1063 | if (len && !buf) |
| 1064 | return -EINVAL; |
| 1065 | |
| 1066 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1067 | return -EINVAL; |
| 1068 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1069 | /* Small page NANDs do not support column change. */ |
| 1070 | if (mtd->writesize <= 512) |
| 1071 | return -ENOTSUPP; |
| 1072 | |
| 1073 | if (chip->exec_op) { |
| 1074 | const struct nand_sdr_timings *sdr = |
| 1075 | nand_get_sdr_timings(&chip->data_interface); |
| 1076 | u8 addrs[2] = {}; |
| 1077 | struct nand_op_instr instrs[] = { |
| 1078 | NAND_OP_CMD(NAND_CMD_RNDOUT, 0), |
| 1079 | NAND_OP_ADDR(2, addrs, 0), |
| 1080 | NAND_OP_CMD(NAND_CMD_RNDOUTSTART, |
| 1081 | PSEC_TO_NSEC(sdr->tCCS_min)), |
| 1082 | NAND_OP_DATA_IN(len, buf, 0), |
| 1083 | }; |
| 1084 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1085 | int ret; |
| 1086 | |
| 1087 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1088 | if (ret < 0) |
| 1089 | return ret; |
| 1090 | |
| 1091 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1092 | if (!len) |
| 1093 | op.ninstrs--; |
| 1094 | |
| 1095 | instrs[3].ctx.data.force_8bit = force_8bit; |
| 1096 | |
| 1097 | return nand_exec_op(chip, &op); |
| 1098 | } |
| 1099 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1100 | chip->legacy.cmdfunc(chip, NAND_CMD_RNDOUT, offset_in_page, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1101 | if (len) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1102 | chip->legacy.read_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1103 | |
| 1104 | return 0; |
| 1105 | } |
| 1106 | EXPORT_SYMBOL_GPL(nand_change_read_column_op); |
| 1107 | |
| 1108 | /** |
| 1109 | * nand_read_oob_op - Do a READ OOB operation |
| 1110 | * @chip: The NAND chip |
| 1111 | * @page: page to read |
| 1112 | * @offset_in_oob: offset within the OOB area |
| 1113 | * @buf: buffer used to store the data |
| 1114 | * @len: length of the buffer |
| 1115 | * |
| 1116 | * This function issues a READ OOB operation. |
| 1117 | * This function does not select/unselect the CS line. |
| 1118 | * |
| 1119 | * Returns 0 on success, a negative error code otherwise. |
| 1120 | */ |
| 1121 | int nand_read_oob_op(struct nand_chip *chip, unsigned int page, |
| 1122 | unsigned int offset_in_oob, void *buf, unsigned int len) |
| 1123 | { |
| 1124 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1125 | |
| 1126 | if (len && !buf) |
| 1127 | return -EINVAL; |
| 1128 | |
| 1129 | if (offset_in_oob + len > mtd->oobsize) |
| 1130 | return -EINVAL; |
| 1131 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1132 | if (chip->exec_op) |
| 1133 | return nand_read_page_op(chip, page, |
| 1134 | mtd->writesize + offset_in_oob, |
| 1135 | buf, len); |
| 1136 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1137 | chip->legacy.cmdfunc(chip, NAND_CMD_READOOB, offset_in_oob, page); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1138 | if (len) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1139 | chip->legacy.read_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1140 | |
| 1141 | return 0; |
| 1142 | } |
| 1143 | EXPORT_SYMBOL_GPL(nand_read_oob_op); |
| 1144 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1145 | static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page, |
| 1146 | unsigned int offset_in_page, const void *buf, |
| 1147 | unsigned int len, bool prog) |
| 1148 | { |
| 1149 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1150 | const struct nand_sdr_timings *sdr = |
| 1151 | nand_get_sdr_timings(&chip->data_interface); |
| 1152 | u8 addrs[5] = {}; |
| 1153 | struct nand_op_instr instrs[] = { |
| 1154 | /* |
| 1155 | * The first instruction will be dropped if we're dealing |
| 1156 | * with a large page NAND and adjusted if we're dealing |
| 1157 | * with a small page NAND and the page offset is > 255. |
| 1158 | */ |
| 1159 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 1160 | NAND_OP_CMD(NAND_CMD_SEQIN, 0), |
| 1161 | NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)), |
| 1162 | NAND_OP_DATA_OUT(len, buf, 0), |
| 1163 | NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1164 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0), |
| 1165 | }; |
| 1166 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1167 | int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1168 | int ret; |
| 1169 | u8 status; |
| 1170 | |
| 1171 | if (naddrs < 0) |
| 1172 | return naddrs; |
| 1173 | |
| 1174 | addrs[naddrs++] = page; |
| 1175 | addrs[naddrs++] = page >> 8; |
| 1176 | if (chip->options & NAND_ROW_ADDR_3) |
| 1177 | addrs[naddrs++] = page >> 16; |
| 1178 | |
| 1179 | instrs[2].ctx.addr.naddrs = naddrs; |
| 1180 | |
| 1181 | /* Drop the last two instructions if we're not programming the page. */ |
| 1182 | if (!prog) { |
| 1183 | op.ninstrs -= 2; |
| 1184 | /* Also drop the DATA_OUT instruction if empty. */ |
| 1185 | if (!len) |
| 1186 | op.ninstrs--; |
| 1187 | } |
| 1188 | |
| 1189 | if (mtd->writesize <= 512) { |
| 1190 | /* |
| 1191 | * Small pages need some more tweaking: we have to adjust the |
| 1192 | * first instruction depending on the page offset we're trying |
| 1193 | * to access. |
| 1194 | */ |
| 1195 | if (offset_in_page >= mtd->writesize) |
| 1196 | instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB; |
| 1197 | else if (offset_in_page >= 256 && |
| 1198 | !(chip->options & NAND_BUSWIDTH_16)) |
| 1199 | instrs[0].ctx.cmd.opcode = NAND_CMD_READ1; |
| 1200 | } else { |
| 1201 | /* |
| 1202 | * Drop the first command if we're dealing with a large page |
| 1203 | * NAND. |
| 1204 | */ |
| 1205 | op.instrs++; |
| 1206 | op.ninstrs--; |
| 1207 | } |
| 1208 | |
| 1209 | ret = nand_exec_op(chip, &op); |
| 1210 | if (!prog || ret) |
| 1211 | return ret; |
| 1212 | |
| 1213 | ret = nand_status_op(chip, &status); |
| 1214 | if (ret) |
| 1215 | return ret; |
| 1216 | |
| 1217 | return status; |
| 1218 | } |
| 1219 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1220 | /** |
| 1221 | * nand_prog_page_begin_op - starts a PROG PAGE operation |
| 1222 | * @chip: The NAND chip |
| 1223 | * @page: page to write |
| 1224 | * @offset_in_page: offset within the page |
| 1225 | * @buf: buffer containing the data to write to the page |
| 1226 | * @len: length of the buffer |
| 1227 | * |
| 1228 | * This function issues the first half of a PROG PAGE operation. |
| 1229 | * This function does not select/unselect the CS line. |
| 1230 | * |
| 1231 | * Returns 0 on success, a negative error code otherwise. |
| 1232 | */ |
| 1233 | int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page, |
| 1234 | unsigned int offset_in_page, const void *buf, |
| 1235 | unsigned int len) |
| 1236 | { |
| 1237 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1238 | |
| 1239 | if (len && !buf) |
| 1240 | return -EINVAL; |
| 1241 | |
| 1242 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1243 | return -EINVAL; |
| 1244 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1245 | if (chip->exec_op) |
| 1246 | return nand_exec_prog_page_op(chip, page, offset_in_page, buf, |
| 1247 | len, false); |
| 1248 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1249 | chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1250 | |
| 1251 | if (buf) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1252 | chip->legacy.write_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1253 | |
| 1254 | return 0; |
| 1255 | } |
| 1256 | EXPORT_SYMBOL_GPL(nand_prog_page_begin_op); |
| 1257 | |
| 1258 | /** |
| 1259 | * nand_prog_page_end_op - ends a PROG PAGE operation |
| 1260 | * @chip: The NAND chip |
| 1261 | * |
| 1262 | * This function issues the second half of a PROG PAGE operation. |
| 1263 | * This function does not select/unselect the CS line. |
| 1264 | * |
| 1265 | * Returns 0 on success, a negative error code otherwise. |
| 1266 | */ |
| 1267 | int nand_prog_page_end_op(struct nand_chip *chip) |
| 1268 | { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1269 | int ret; |
| 1270 | u8 status; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1271 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1272 | if (chip->exec_op) { |
| 1273 | const struct nand_sdr_timings *sdr = |
| 1274 | nand_get_sdr_timings(&chip->data_interface); |
| 1275 | struct nand_op_instr instrs[] = { |
| 1276 | NAND_OP_CMD(NAND_CMD_PAGEPROG, |
| 1277 | PSEC_TO_NSEC(sdr->tWB_max)), |
| 1278 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0), |
| 1279 | }; |
| 1280 | struct nand_operation op = NAND_OPERATION(instrs); |
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 | ret = nand_exec_op(chip, &op); |
| 1283 | if (ret) |
| 1284 | return ret; |
| 1285 | |
| 1286 | ret = nand_status_op(chip, &status); |
| 1287 | if (ret) |
| 1288 | return ret; |
| 1289 | } else { |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1290 | chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1); |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1291 | ret = chip->legacy.waitfunc(chip); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1292 | if (ret < 0) |
| 1293 | return ret; |
| 1294 | |
| 1295 | status = ret; |
| 1296 | } |
| 1297 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1298 | if (status & NAND_STATUS_FAIL) |
| 1299 | return -EIO; |
| 1300 | |
| 1301 | return 0; |
| 1302 | } |
| 1303 | EXPORT_SYMBOL_GPL(nand_prog_page_end_op); |
| 1304 | |
| 1305 | /** |
| 1306 | * nand_prog_page_op - Do a full PROG PAGE operation |
| 1307 | * @chip: The NAND chip |
| 1308 | * @page: page to write |
| 1309 | * @offset_in_page: offset within the page |
| 1310 | * @buf: buffer containing the data to write to the page |
| 1311 | * @len: length of the buffer |
| 1312 | * |
| 1313 | * This function issues a full PROG PAGE operation. |
| 1314 | * This function does not select/unselect the CS line. |
| 1315 | * |
| 1316 | * Returns 0 on success, a negative error code otherwise. |
| 1317 | */ |
| 1318 | int nand_prog_page_op(struct nand_chip *chip, unsigned int page, |
| 1319 | unsigned int offset_in_page, const void *buf, |
| 1320 | unsigned int len) |
| 1321 | { |
| 1322 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1323 | int status; |
| 1324 | |
| 1325 | if (!len || !buf) |
| 1326 | return -EINVAL; |
| 1327 | |
| 1328 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1329 | return -EINVAL; |
| 1330 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1331 | if (chip->exec_op) { |
| 1332 | status = nand_exec_prog_page_op(chip, page, offset_in_page, buf, |
| 1333 | len, true); |
| 1334 | } else { |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1335 | chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, |
| 1336 | page); |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1337 | chip->legacy.write_buf(chip, buf, len); |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1338 | chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1); |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1339 | status = chip->legacy.waitfunc(chip); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1340 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1341 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1342 | if (status & NAND_STATUS_FAIL) |
| 1343 | return -EIO; |
| 1344 | |
| 1345 | return 0; |
| 1346 | } |
| 1347 | EXPORT_SYMBOL_GPL(nand_prog_page_op); |
| 1348 | |
| 1349 | /** |
| 1350 | * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation |
| 1351 | * @chip: The NAND chip |
| 1352 | * @offset_in_page: offset within the page |
| 1353 | * @buf: buffer containing the data to send to the NAND |
| 1354 | * @len: length of the buffer |
| 1355 | * @force_8bit: force 8-bit bus access |
| 1356 | * |
| 1357 | * This function issues a CHANGE WRITE COLUMN operation. |
| 1358 | * This function does not select/unselect the CS line. |
| 1359 | * |
| 1360 | * Returns 0 on success, a negative error code otherwise. |
| 1361 | */ |
| 1362 | int nand_change_write_column_op(struct nand_chip *chip, |
| 1363 | unsigned int offset_in_page, |
| 1364 | const void *buf, unsigned int len, |
| 1365 | bool force_8bit) |
| 1366 | { |
| 1367 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1368 | |
| 1369 | if (len && !buf) |
| 1370 | return -EINVAL; |
| 1371 | |
| 1372 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1373 | return -EINVAL; |
| 1374 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1375 | /* Small page NANDs do not support column change. */ |
| 1376 | if (mtd->writesize <= 512) |
| 1377 | return -ENOTSUPP; |
| 1378 | |
| 1379 | if (chip->exec_op) { |
| 1380 | const struct nand_sdr_timings *sdr = |
| 1381 | nand_get_sdr_timings(&chip->data_interface); |
| 1382 | u8 addrs[2]; |
| 1383 | struct nand_op_instr instrs[] = { |
| 1384 | NAND_OP_CMD(NAND_CMD_RNDIN, 0), |
| 1385 | NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)), |
| 1386 | NAND_OP_DATA_OUT(len, buf, 0), |
| 1387 | }; |
| 1388 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1389 | int ret; |
| 1390 | |
| 1391 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1392 | if (ret < 0) |
| 1393 | return ret; |
| 1394 | |
| 1395 | instrs[2].ctx.data.force_8bit = force_8bit; |
| 1396 | |
| 1397 | /* Drop the DATA_OUT instruction if len is set to 0. */ |
| 1398 | if (!len) |
| 1399 | op.ninstrs--; |
| 1400 | |
| 1401 | return nand_exec_op(chip, &op); |
| 1402 | } |
| 1403 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1404 | chip->legacy.cmdfunc(chip, NAND_CMD_RNDIN, offset_in_page, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1405 | if (len) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1406 | chip->legacy.write_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1407 | |
| 1408 | return 0; |
| 1409 | } |
| 1410 | EXPORT_SYMBOL_GPL(nand_change_write_column_op); |
| 1411 | |
| 1412 | /** |
| 1413 | * nand_readid_op - Do a READID operation |
| 1414 | * @chip: The NAND chip |
| 1415 | * @addr: address cycle to pass after the READID command |
| 1416 | * @buf: buffer used to store the ID |
| 1417 | * @len: length of the buffer |
| 1418 | * |
| 1419 | * This function sends a READID command and reads back the ID returned by the |
| 1420 | * NAND. |
| 1421 | * This function does not select/unselect the CS line. |
| 1422 | * |
| 1423 | * Returns 0 on success, a negative error code otherwise. |
| 1424 | */ |
| 1425 | int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf, |
| 1426 | unsigned int len) |
| 1427 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1428 | unsigned int i; |
| 1429 | u8 *id = buf; |
| 1430 | |
| 1431 | if (len && !buf) |
| 1432 | return -EINVAL; |
| 1433 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1434 | if (chip->exec_op) { |
| 1435 | const struct nand_sdr_timings *sdr = |
| 1436 | nand_get_sdr_timings(&chip->data_interface); |
| 1437 | struct nand_op_instr instrs[] = { |
| 1438 | NAND_OP_CMD(NAND_CMD_READID, 0), |
| 1439 | NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)), |
| 1440 | NAND_OP_8BIT_DATA_IN(len, buf, 0), |
| 1441 | }; |
| 1442 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1443 | |
| 1444 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1445 | if (!len) |
| 1446 | op.ninstrs--; |
| 1447 | |
| 1448 | return nand_exec_op(chip, &op); |
| 1449 | } |
| 1450 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1451 | chip->legacy.cmdfunc(chip, NAND_CMD_READID, addr, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1452 | |
| 1453 | for (i = 0; i < len; i++) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1454 | id[i] = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1455 | |
| 1456 | return 0; |
| 1457 | } |
| 1458 | EXPORT_SYMBOL_GPL(nand_readid_op); |
| 1459 | |
| 1460 | /** |
| 1461 | * nand_status_op - Do a STATUS operation |
| 1462 | * @chip: The NAND chip |
| 1463 | * @status: out variable to store the NAND status |
| 1464 | * |
| 1465 | * This function sends a STATUS command and reads back the status returned by |
| 1466 | * the NAND. |
| 1467 | * This function does not select/unselect the CS line. |
| 1468 | * |
| 1469 | * Returns 0 on success, a negative error code otherwise. |
| 1470 | */ |
| 1471 | int nand_status_op(struct nand_chip *chip, u8 *status) |
| 1472 | { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1473 | if (chip->exec_op) { |
| 1474 | const struct nand_sdr_timings *sdr = |
| 1475 | nand_get_sdr_timings(&chip->data_interface); |
| 1476 | struct nand_op_instr instrs[] = { |
| 1477 | NAND_OP_CMD(NAND_CMD_STATUS, |
| 1478 | PSEC_TO_NSEC(sdr->tADL_min)), |
| 1479 | NAND_OP_8BIT_DATA_IN(1, status, 0), |
| 1480 | }; |
| 1481 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1482 | |
| 1483 | if (!status) |
| 1484 | op.ninstrs--; |
| 1485 | |
| 1486 | return nand_exec_op(chip, &op); |
| 1487 | } |
| 1488 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1489 | chip->legacy.cmdfunc(chip, NAND_CMD_STATUS, -1, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1490 | if (status) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1491 | *status = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1492 | |
| 1493 | return 0; |
| 1494 | } |
| 1495 | EXPORT_SYMBOL_GPL(nand_status_op); |
| 1496 | |
| 1497 | /** |
| 1498 | * nand_exit_status_op - Exit a STATUS operation |
| 1499 | * @chip: The NAND chip |
| 1500 | * |
| 1501 | * This function sends a READ0 command to cancel the effect of the STATUS |
| 1502 | * command to avoid reading only the status until a new read command is sent. |
| 1503 | * |
| 1504 | * This function does not select/unselect the CS line. |
| 1505 | * |
| 1506 | * Returns 0 on success, a negative error code otherwise. |
| 1507 | */ |
| 1508 | int nand_exit_status_op(struct nand_chip *chip) |
| 1509 | { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1510 | if (chip->exec_op) { |
| 1511 | struct nand_op_instr instrs[] = { |
| 1512 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 1513 | }; |
| 1514 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1515 | |
| 1516 | return nand_exec_op(chip, &op); |
| 1517 | } |
| 1518 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1519 | chip->legacy.cmdfunc(chip, NAND_CMD_READ0, -1, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1520 | |
| 1521 | return 0; |
| 1522 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1523 | |
| 1524 | /** |
| 1525 | * nand_erase_op - Do an erase operation |
| 1526 | * @chip: The NAND chip |
| 1527 | * @eraseblock: block to erase |
| 1528 | * |
| 1529 | * This function sends an ERASE command and waits for the NAND to be ready |
| 1530 | * before returning. |
| 1531 | * This function does not select/unselect the CS line. |
| 1532 | * |
| 1533 | * Returns 0 on success, a negative error code otherwise. |
| 1534 | */ |
| 1535 | int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock) |
| 1536 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1537 | unsigned int page = eraseblock << |
| 1538 | (chip->phys_erase_shift - chip->page_shift); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1539 | int ret; |
| 1540 | u8 status; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1541 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1542 | if (chip->exec_op) { |
| 1543 | const struct nand_sdr_timings *sdr = |
| 1544 | nand_get_sdr_timings(&chip->data_interface); |
| 1545 | u8 addrs[3] = { page, page >> 8, page >> 16 }; |
| 1546 | struct nand_op_instr instrs[] = { |
| 1547 | NAND_OP_CMD(NAND_CMD_ERASE1, 0), |
| 1548 | NAND_OP_ADDR(2, addrs, 0), |
| 1549 | NAND_OP_CMD(NAND_CMD_ERASE2, |
| 1550 | PSEC_TO_MSEC(sdr->tWB_max)), |
| 1551 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0), |
| 1552 | }; |
| 1553 | struct nand_operation op = NAND_OPERATION(instrs); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1554 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1555 | if (chip->options & NAND_ROW_ADDR_3) |
| 1556 | instrs[1].ctx.addr.naddrs++; |
| 1557 | |
| 1558 | ret = nand_exec_op(chip, &op); |
| 1559 | if (ret) |
| 1560 | return ret; |
| 1561 | |
| 1562 | ret = nand_status_op(chip, &status); |
| 1563 | if (ret) |
| 1564 | return ret; |
| 1565 | } else { |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1566 | chip->legacy.cmdfunc(chip, NAND_CMD_ERASE1, -1, page); |
| 1567 | chip->legacy.cmdfunc(chip, NAND_CMD_ERASE2, -1, -1); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1568 | |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1569 | ret = chip->legacy.waitfunc(chip); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1570 | if (ret < 0) |
| 1571 | return ret; |
| 1572 | |
| 1573 | status = ret; |
| 1574 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1575 | |
| 1576 | if (status & NAND_STATUS_FAIL) |
| 1577 | return -EIO; |
| 1578 | |
| 1579 | return 0; |
| 1580 | } |
| 1581 | EXPORT_SYMBOL_GPL(nand_erase_op); |
| 1582 | |
| 1583 | /** |
| 1584 | * nand_set_features_op - Do a SET FEATURES operation |
| 1585 | * @chip: The NAND chip |
| 1586 | * @feature: feature id |
| 1587 | * @data: 4 bytes of data |
| 1588 | * |
| 1589 | * This function sends a SET FEATURES command and waits for the NAND to be |
| 1590 | * ready before returning. |
| 1591 | * This function does not select/unselect the CS line. |
| 1592 | * |
| 1593 | * Returns 0 on success, a negative error code otherwise. |
| 1594 | */ |
| 1595 | static int nand_set_features_op(struct nand_chip *chip, u8 feature, |
| 1596 | const void *data) |
| 1597 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1598 | const u8 *params = data; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1599 | int i, ret; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1600 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1601 | if (chip->exec_op) { |
| 1602 | const struct nand_sdr_timings *sdr = |
| 1603 | nand_get_sdr_timings(&chip->data_interface); |
| 1604 | struct nand_op_instr instrs[] = { |
| 1605 | NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0), |
| 1606 | NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)), |
| 1607 | NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data, |
| 1608 | PSEC_TO_NSEC(sdr->tWB_max)), |
| 1609 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0), |
| 1610 | }; |
| 1611 | struct nand_operation op = NAND_OPERATION(instrs); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1612 | |
Boris Brezillon | 782d196 | 2018-05-11 14:44:07 +0200 | [diff] [blame] | 1613 | return nand_exec_op(chip, &op); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1614 | } |
| 1615 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1616 | chip->legacy.cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1); |
Boris Brezillon | 782d196 | 2018-05-11 14:44:07 +0200 | [diff] [blame] | 1617 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1618 | chip->legacy.write_byte(chip, params[i]); |
Boris Brezillon | 782d196 | 2018-05-11 14:44:07 +0200 | [diff] [blame] | 1619 | |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1620 | ret = chip->legacy.waitfunc(chip); |
Boris Brezillon | 782d196 | 2018-05-11 14:44:07 +0200 | [diff] [blame] | 1621 | if (ret < 0) |
| 1622 | return ret; |
| 1623 | |
| 1624 | if (ret & NAND_STATUS_FAIL) |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1625 | return -EIO; |
| 1626 | |
| 1627 | return 0; |
| 1628 | } |
| 1629 | |
| 1630 | /** |
| 1631 | * nand_get_features_op - Do a GET FEATURES operation |
| 1632 | * @chip: The NAND chip |
| 1633 | * @feature: feature id |
| 1634 | * @data: 4 bytes of data |
| 1635 | * |
| 1636 | * This function sends a GET FEATURES command and waits for the NAND to be |
| 1637 | * ready before returning. |
| 1638 | * This function does not select/unselect the CS line. |
| 1639 | * |
| 1640 | * Returns 0 on success, a negative error code otherwise. |
| 1641 | */ |
| 1642 | static int nand_get_features_op(struct nand_chip *chip, u8 feature, |
| 1643 | void *data) |
| 1644 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1645 | u8 *params = data; |
| 1646 | int i; |
| 1647 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1648 | if (chip->exec_op) { |
| 1649 | const struct nand_sdr_timings *sdr = |
| 1650 | nand_get_sdr_timings(&chip->data_interface); |
| 1651 | struct nand_op_instr instrs[] = { |
| 1652 | NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0), |
| 1653 | NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1654 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), |
| 1655 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 1656 | NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN, |
| 1657 | data, 0), |
| 1658 | }; |
| 1659 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1660 | |
| 1661 | return nand_exec_op(chip, &op); |
| 1662 | } |
| 1663 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1664 | chip->legacy.cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1665 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1666 | params[i] = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1667 | |
| 1668 | return 0; |
| 1669 | } |
| 1670 | |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 1671 | static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms, |
| 1672 | unsigned int delay_ns) |
| 1673 | { |
| 1674 | if (chip->exec_op) { |
| 1675 | struct nand_op_instr instrs[] = { |
| 1676 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms), |
| 1677 | PSEC_TO_NSEC(delay_ns)), |
| 1678 | }; |
| 1679 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1680 | |
| 1681 | return nand_exec_op(chip, &op); |
| 1682 | } |
| 1683 | |
| 1684 | /* Apply delay or wait for ready/busy pin */ |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1685 | if (!chip->legacy.dev_ready) |
Boris Brezillon | 3cece3a | 2018-09-07 00:38:41 +0200 | [diff] [blame] | 1686 | udelay(chip->legacy.chip_delay); |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 1687 | else |
Boris Brezillon | 2b356ab | 2018-09-06 14:05:16 +0200 | [diff] [blame] | 1688 | nand_wait_ready(chip); |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 1689 | |
| 1690 | return 0; |
| 1691 | } |
| 1692 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1693 | /** |
| 1694 | * nand_reset_op - Do a reset operation |
| 1695 | * @chip: The NAND chip |
| 1696 | * |
| 1697 | * This function sends a RESET command and waits for the NAND to be ready |
| 1698 | * before returning. |
| 1699 | * This function does not select/unselect the CS line. |
| 1700 | * |
| 1701 | * Returns 0 on success, a negative error code otherwise. |
| 1702 | */ |
| 1703 | int nand_reset_op(struct nand_chip *chip) |
| 1704 | { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1705 | if (chip->exec_op) { |
| 1706 | const struct nand_sdr_timings *sdr = |
| 1707 | nand_get_sdr_timings(&chip->data_interface); |
| 1708 | struct nand_op_instr instrs[] = { |
| 1709 | NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1710 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0), |
| 1711 | }; |
| 1712 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1713 | |
| 1714 | return nand_exec_op(chip, &op); |
| 1715 | } |
| 1716 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1717 | chip->legacy.cmdfunc(chip, NAND_CMD_RESET, -1, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1718 | |
| 1719 | return 0; |
| 1720 | } |
| 1721 | EXPORT_SYMBOL_GPL(nand_reset_op); |
| 1722 | |
| 1723 | /** |
| 1724 | * nand_read_data_op - Read data from the NAND |
| 1725 | * @chip: The NAND chip |
| 1726 | * @buf: buffer used to store the data |
| 1727 | * @len: length of the buffer |
| 1728 | * @force_8bit: force 8-bit bus access |
| 1729 | * |
| 1730 | * This function does a raw data read on the bus. Usually used after launching |
| 1731 | * another NAND operation like nand_read_page_op(). |
| 1732 | * This function does not select/unselect the CS line. |
| 1733 | * |
| 1734 | * Returns 0 on success, a negative error code otherwise. |
| 1735 | */ |
| 1736 | int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len, |
| 1737 | bool force_8bit) |
| 1738 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1739 | if (!len || !buf) |
| 1740 | return -EINVAL; |
| 1741 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1742 | if (chip->exec_op) { |
| 1743 | struct nand_op_instr instrs[] = { |
| 1744 | NAND_OP_DATA_IN(len, buf, 0), |
| 1745 | }; |
| 1746 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1747 | |
| 1748 | instrs[0].ctx.data.force_8bit = force_8bit; |
| 1749 | |
| 1750 | return nand_exec_op(chip, &op); |
| 1751 | } |
| 1752 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1753 | if (force_8bit) { |
| 1754 | u8 *p = buf; |
| 1755 | unsigned int i; |
| 1756 | |
| 1757 | for (i = 0; i < len; i++) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1758 | p[i] = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1759 | } else { |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1760 | chip->legacy.read_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | return 0; |
| 1764 | } |
| 1765 | EXPORT_SYMBOL_GPL(nand_read_data_op); |
| 1766 | |
| 1767 | /** |
| 1768 | * nand_write_data_op - Write data from the NAND |
| 1769 | * @chip: The NAND chip |
| 1770 | * @buf: buffer containing the data to send on the bus |
| 1771 | * @len: length of the buffer |
| 1772 | * @force_8bit: force 8-bit bus access |
| 1773 | * |
| 1774 | * This function does a raw data write on the bus. Usually used after launching |
| 1775 | * another NAND operation like nand_write_page_begin_op(). |
| 1776 | * This function does not select/unselect the CS line. |
| 1777 | * |
| 1778 | * Returns 0 on success, a negative error code otherwise. |
| 1779 | */ |
| 1780 | int nand_write_data_op(struct nand_chip *chip, const void *buf, |
| 1781 | unsigned int len, bool force_8bit) |
| 1782 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1783 | if (!len || !buf) |
| 1784 | return -EINVAL; |
| 1785 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1786 | if (chip->exec_op) { |
| 1787 | struct nand_op_instr instrs[] = { |
| 1788 | NAND_OP_DATA_OUT(len, buf, 0), |
| 1789 | }; |
| 1790 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1791 | |
| 1792 | instrs[0].ctx.data.force_8bit = force_8bit; |
| 1793 | |
| 1794 | return nand_exec_op(chip, &op); |
| 1795 | } |
| 1796 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1797 | if (force_8bit) { |
| 1798 | const u8 *p = buf; |
| 1799 | unsigned int i; |
| 1800 | |
| 1801 | for (i = 0; i < len; i++) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1802 | chip->legacy.write_byte(chip, p[i]); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1803 | } else { |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1804 | chip->legacy.write_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | return 0; |
| 1808 | } |
| 1809 | EXPORT_SYMBOL_GPL(nand_write_data_op); |
| 1810 | |
| 1811 | /** |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1812 | * struct nand_op_parser_ctx - Context used by the parser |
| 1813 | * @instrs: array of all the instructions that must be addressed |
| 1814 | * @ninstrs: length of the @instrs array |
| 1815 | * @subop: Sub-operation to be passed to the NAND controller |
| 1816 | * |
| 1817 | * This structure is used by the core to split NAND operations into |
| 1818 | * sub-operations that can be handled by the NAND controller. |
| 1819 | */ |
| 1820 | struct nand_op_parser_ctx { |
| 1821 | const struct nand_op_instr *instrs; |
| 1822 | unsigned int ninstrs; |
| 1823 | struct nand_subop subop; |
| 1824 | }; |
| 1825 | |
| 1826 | /** |
| 1827 | * nand_op_parser_must_split_instr - Checks if an instruction must be split |
| 1828 | * @pat: the parser pattern element that matches @instr |
| 1829 | * @instr: pointer to the instruction to check |
| 1830 | * @start_offset: this is an in/out parameter. If @instr has already been |
| 1831 | * split, then @start_offset is the offset from which to start |
| 1832 | * (either an address cycle or an offset in the data buffer). |
| 1833 | * Conversely, if the function returns true (ie. instr must be |
| 1834 | * split), this parameter is updated to point to the first |
| 1835 | * data/address cycle that has not been taken care of. |
| 1836 | * |
| 1837 | * Some NAND controllers are limited and cannot send X address cycles with a |
| 1838 | * unique operation, or cannot read/write more than Y bytes at the same time. |
| 1839 | * In this case, split the instruction that does not fit in a single |
| 1840 | * controller-operation into two or more chunks. |
| 1841 | * |
| 1842 | * Returns true if the instruction must be split, false otherwise. |
| 1843 | * The @start_offset parameter is also updated to the offset at which the next |
| 1844 | * bundle of instruction must start (if an address or a data instruction). |
| 1845 | */ |
| 1846 | static bool |
| 1847 | nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat, |
| 1848 | const struct nand_op_instr *instr, |
| 1849 | unsigned int *start_offset) |
| 1850 | { |
| 1851 | switch (pat->type) { |
| 1852 | case NAND_OP_ADDR_INSTR: |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 1853 | if (!pat->ctx.addr.maxcycles) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1854 | break; |
| 1855 | |
| 1856 | if (instr->ctx.addr.naddrs - *start_offset > |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 1857 | pat->ctx.addr.maxcycles) { |
| 1858 | *start_offset += pat->ctx.addr.maxcycles; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1859 | return true; |
| 1860 | } |
| 1861 | break; |
| 1862 | |
| 1863 | case NAND_OP_DATA_IN_INSTR: |
| 1864 | case NAND_OP_DATA_OUT_INSTR: |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 1865 | if (!pat->ctx.data.maxlen) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1866 | break; |
| 1867 | |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 1868 | if (instr->ctx.data.len - *start_offset > |
| 1869 | pat->ctx.data.maxlen) { |
| 1870 | *start_offset += pat->ctx.data.maxlen; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1871 | return true; |
| 1872 | } |
| 1873 | break; |
| 1874 | |
| 1875 | default: |
| 1876 | break; |
| 1877 | } |
| 1878 | |
| 1879 | return false; |
| 1880 | } |
| 1881 | |
| 1882 | /** |
| 1883 | * nand_op_parser_match_pat - Checks if a pattern matches the instructions |
| 1884 | * remaining in the parser context |
| 1885 | * @pat: the pattern to test |
| 1886 | * @ctx: the parser context structure to match with the pattern @pat |
| 1887 | * |
| 1888 | * Check if @pat matches the set or a sub-set of instructions remaining in @ctx. |
| 1889 | * Returns true if this is the case, false ortherwise. When true is returned, |
| 1890 | * @ctx->subop is updated with the set of instructions to be passed to the |
| 1891 | * controller driver. |
| 1892 | */ |
| 1893 | static bool |
| 1894 | nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat, |
| 1895 | struct nand_op_parser_ctx *ctx) |
| 1896 | { |
| 1897 | unsigned int instr_offset = ctx->subop.first_instr_start_off; |
| 1898 | const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs; |
| 1899 | const struct nand_op_instr *instr = ctx->subop.instrs; |
| 1900 | unsigned int i, ninstrs; |
| 1901 | |
| 1902 | for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) { |
| 1903 | /* |
| 1904 | * The pattern instruction does not match the operation |
| 1905 | * instruction. If the instruction is marked optional in the |
| 1906 | * pattern definition, we skip the pattern element and continue |
| 1907 | * to the next one. If the element is mandatory, there's no |
| 1908 | * match and we can return false directly. |
| 1909 | */ |
| 1910 | if (instr->type != pat->elems[i].type) { |
| 1911 | if (!pat->elems[i].optional) |
| 1912 | return false; |
| 1913 | |
| 1914 | continue; |
| 1915 | } |
| 1916 | |
| 1917 | /* |
| 1918 | * Now check the pattern element constraints. If the pattern is |
| 1919 | * not able to handle the whole instruction in a single step, |
| 1920 | * we have to split it. |
| 1921 | * The last_instr_end_off value comes back updated to point to |
| 1922 | * the position where we have to split the instruction (the |
| 1923 | * start of the next subop chunk). |
| 1924 | */ |
| 1925 | if (nand_op_parser_must_split_instr(&pat->elems[i], instr, |
| 1926 | &instr_offset)) { |
| 1927 | ninstrs++; |
| 1928 | i++; |
| 1929 | break; |
| 1930 | } |
| 1931 | |
| 1932 | instr++; |
| 1933 | ninstrs++; |
| 1934 | instr_offset = 0; |
| 1935 | } |
| 1936 | |
| 1937 | /* |
| 1938 | * This can happen if all instructions of a pattern are optional. |
| 1939 | * Still, if there's not at least one instruction handled by this |
| 1940 | * pattern, this is not a match, and we should try the next one (if |
| 1941 | * any). |
| 1942 | */ |
| 1943 | if (!ninstrs) |
| 1944 | return false; |
| 1945 | |
| 1946 | /* |
| 1947 | * We had a match on the pattern head, but the pattern may be longer |
| 1948 | * than the instructions we're asked to execute. We need to make sure |
| 1949 | * there's no mandatory elements in the pattern tail. |
| 1950 | */ |
| 1951 | for (; i < pat->nelems; i++) { |
| 1952 | if (!pat->elems[i].optional) |
| 1953 | return false; |
| 1954 | } |
| 1955 | |
| 1956 | /* |
| 1957 | * We have a match: update the subop structure accordingly and return |
| 1958 | * true. |
| 1959 | */ |
| 1960 | ctx->subop.ninstrs = ninstrs; |
| 1961 | ctx->subop.last_instr_end_off = instr_offset; |
| 1962 | |
| 1963 | return true; |
| 1964 | } |
| 1965 | |
| 1966 | #if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG) |
| 1967 | static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx) |
| 1968 | { |
| 1969 | const struct nand_op_instr *instr; |
| 1970 | char *prefix = " "; |
| 1971 | unsigned int i; |
| 1972 | |
| 1973 | pr_debug("executing subop:\n"); |
| 1974 | |
| 1975 | for (i = 0; i < ctx->ninstrs; i++) { |
| 1976 | instr = &ctx->instrs[i]; |
| 1977 | |
| 1978 | if (instr == &ctx->subop.instrs[0]) |
| 1979 | prefix = " ->"; |
| 1980 | |
| 1981 | switch (instr->type) { |
| 1982 | case NAND_OP_CMD_INSTR: |
| 1983 | pr_debug("%sCMD [0x%02x]\n", prefix, |
| 1984 | instr->ctx.cmd.opcode); |
| 1985 | break; |
| 1986 | case NAND_OP_ADDR_INSTR: |
| 1987 | pr_debug("%sADDR [%d cyc: %*ph]\n", prefix, |
| 1988 | instr->ctx.addr.naddrs, |
| 1989 | instr->ctx.addr.naddrs < 64 ? |
| 1990 | instr->ctx.addr.naddrs : 64, |
| 1991 | instr->ctx.addr.addrs); |
| 1992 | break; |
| 1993 | case NAND_OP_DATA_IN_INSTR: |
| 1994 | pr_debug("%sDATA_IN [%d B%s]\n", prefix, |
| 1995 | instr->ctx.data.len, |
| 1996 | instr->ctx.data.force_8bit ? |
| 1997 | ", force 8-bit" : ""); |
| 1998 | break; |
| 1999 | case NAND_OP_DATA_OUT_INSTR: |
| 2000 | pr_debug("%sDATA_OUT [%d B%s]\n", prefix, |
| 2001 | instr->ctx.data.len, |
| 2002 | instr->ctx.data.force_8bit ? |
| 2003 | ", force 8-bit" : ""); |
| 2004 | break; |
| 2005 | case NAND_OP_WAITRDY_INSTR: |
| 2006 | pr_debug("%sWAITRDY [max %d ms]\n", prefix, |
| 2007 | instr->ctx.waitrdy.timeout_ms); |
| 2008 | break; |
| 2009 | } |
| 2010 | |
| 2011 | if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1]) |
| 2012 | prefix = " "; |
| 2013 | } |
| 2014 | } |
| 2015 | #else |
| 2016 | static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx) |
| 2017 | { |
| 2018 | /* NOP */ |
| 2019 | } |
| 2020 | #endif |
| 2021 | |
| 2022 | /** |
| 2023 | * nand_op_parser_exec_op - exec_op parser |
| 2024 | * @chip: the NAND chip |
| 2025 | * @parser: patterns description provided by the controller driver |
| 2026 | * @op: the NAND operation to address |
| 2027 | * @check_only: when true, the function only checks if @op can be handled but |
| 2028 | * does not execute the operation |
| 2029 | * |
| 2030 | * Helper function designed to ease integration of NAND controller drivers that |
| 2031 | * only support a limited set of instruction sequences. The supported sequences |
| 2032 | * are described in @parser, and the framework takes care of splitting @op into |
| 2033 | * multiple sub-operations (if required) and pass them back to the ->exec() |
| 2034 | * callback of the matching pattern if @check_only is set to false. |
| 2035 | * |
| 2036 | * NAND controller drivers should call this function from their own ->exec_op() |
| 2037 | * implementation. |
| 2038 | * |
| 2039 | * Returns 0 on success, a negative error code otherwise. A failure can be |
| 2040 | * caused by an unsupported operation (none of the supported patterns is able |
| 2041 | * to handle the requested operation), or an error returned by one of the |
| 2042 | * matching pattern->exec() hook. |
| 2043 | */ |
| 2044 | int nand_op_parser_exec_op(struct nand_chip *chip, |
| 2045 | const struct nand_op_parser *parser, |
| 2046 | const struct nand_operation *op, bool check_only) |
| 2047 | { |
| 2048 | struct nand_op_parser_ctx ctx = { |
| 2049 | .subop.instrs = op->instrs, |
| 2050 | .instrs = op->instrs, |
| 2051 | .ninstrs = op->ninstrs, |
| 2052 | }; |
| 2053 | unsigned int i; |
| 2054 | |
| 2055 | while (ctx.subop.instrs < op->instrs + op->ninstrs) { |
| 2056 | int ret; |
| 2057 | |
| 2058 | for (i = 0; i < parser->npatterns; i++) { |
| 2059 | const struct nand_op_parser_pattern *pattern; |
| 2060 | |
| 2061 | pattern = &parser->patterns[i]; |
| 2062 | if (!nand_op_parser_match_pat(pattern, &ctx)) |
| 2063 | continue; |
| 2064 | |
| 2065 | nand_op_parser_trace(&ctx); |
| 2066 | |
| 2067 | if (check_only) |
| 2068 | break; |
| 2069 | |
| 2070 | ret = pattern->exec(chip, &ctx.subop); |
| 2071 | if (ret) |
| 2072 | return ret; |
| 2073 | |
| 2074 | break; |
| 2075 | } |
| 2076 | |
| 2077 | if (i == parser->npatterns) { |
| 2078 | pr_debug("->exec_op() parser: pattern not found!\n"); |
| 2079 | return -ENOTSUPP; |
| 2080 | } |
| 2081 | |
| 2082 | /* |
| 2083 | * Update the context structure by pointing to the start of the |
| 2084 | * next subop. |
| 2085 | */ |
| 2086 | ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs; |
| 2087 | if (ctx.subop.last_instr_end_off) |
| 2088 | ctx.subop.instrs -= 1; |
| 2089 | |
| 2090 | ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off; |
| 2091 | } |
| 2092 | |
| 2093 | return 0; |
| 2094 | } |
| 2095 | EXPORT_SYMBOL_GPL(nand_op_parser_exec_op); |
| 2096 | |
| 2097 | static bool nand_instr_is_data(const struct nand_op_instr *instr) |
| 2098 | { |
| 2099 | return instr && (instr->type == NAND_OP_DATA_IN_INSTR || |
| 2100 | instr->type == NAND_OP_DATA_OUT_INSTR); |
| 2101 | } |
| 2102 | |
| 2103 | static bool nand_subop_instr_is_valid(const struct nand_subop *subop, |
| 2104 | unsigned int instr_idx) |
| 2105 | { |
| 2106 | return subop && instr_idx < subop->ninstrs; |
| 2107 | } |
| 2108 | |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2109 | static unsigned int nand_subop_get_start_off(const struct nand_subop *subop, |
| 2110 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2111 | { |
| 2112 | if (instr_idx) |
| 2113 | return 0; |
| 2114 | |
| 2115 | return subop->first_instr_start_off; |
| 2116 | } |
| 2117 | |
| 2118 | /** |
| 2119 | * nand_subop_get_addr_start_off - Get the start offset in an address array |
| 2120 | * @subop: The entire sub-operation |
| 2121 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2122 | * |
| 2123 | * During driver development, one could be tempted to directly use the |
| 2124 | * ->addr.addrs field of address instructions. This is wrong as address |
| 2125 | * instructions might be split. |
| 2126 | * |
| 2127 | * Given an address instruction, returns the offset of the first cycle to issue. |
| 2128 | */ |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2129 | unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop, |
| 2130 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2131 | { |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2132 | if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2133 | subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)) |
| 2134 | return 0; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2135 | |
| 2136 | return nand_subop_get_start_off(subop, instr_idx); |
| 2137 | } |
| 2138 | EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off); |
| 2139 | |
| 2140 | /** |
| 2141 | * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert |
| 2142 | * @subop: The entire sub-operation |
| 2143 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2144 | * |
| 2145 | * During driver development, one could be tempted to directly use the |
| 2146 | * ->addr->naddrs field of a data instruction. This is wrong as instructions |
| 2147 | * might be split. |
| 2148 | * |
| 2149 | * Given an address instruction, returns the number of address cycle to issue. |
| 2150 | */ |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2151 | unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop, |
| 2152 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2153 | { |
| 2154 | int start_off, end_off; |
| 2155 | |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2156 | if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2157 | subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)) |
| 2158 | return 0; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2159 | |
| 2160 | start_off = nand_subop_get_addr_start_off(subop, instr_idx); |
| 2161 | |
| 2162 | if (instr_idx == subop->ninstrs - 1 && |
| 2163 | subop->last_instr_end_off) |
| 2164 | end_off = subop->last_instr_end_off; |
| 2165 | else |
| 2166 | end_off = subop->instrs[instr_idx].ctx.addr.naddrs; |
| 2167 | |
| 2168 | return end_off - start_off; |
| 2169 | } |
| 2170 | EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc); |
| 2171 | |
| 2172 | /** |
| 2173 | * nand_subop_get_data_start_off - Get the start offset in a data array |
| 2174 | * @subop: The entire sub-operation |
| 2175 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2176 | * |
| 2177 | * During driver development, one could be tempted to directly use the |
| 2178 | * ->data->buf.{in,out} field of data instructions. This is wrong as data |
| 2179 | * instructions might be split. |
| 2180 | * |
| 2181 | * Given a data instruction, returns the offset to start from. |
| 2182 | */ |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2183 | unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop, |
| 2184 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2185 | { |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2186 | if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2187 | !nand_instr_is_data(&subop->instrs[instr_idx]))) |
| 2188 | return 0; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2189 | |
| 2190 | return nand_subop_get_start_off(subop, instr_idx); |
| 2191 | } |
| 2192 | EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off); |
| 2193 | |
| 2194 | /** |
| 2195 | * nand_subop_get_data_len - Get the number of bytes to retrieve |
| 2196 | * @subop: The entire sub-operation |
| 2197 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2198 | * |
| 2199 | * During driver development, one could be tempted to directly use the |
| 2200 | * ->data->len field of a data instruction. This is wrong as data instructions |
| 2201 | * might be split. |
| 2202 | * |
| 2203 | * Returns the length of the chunk of data to send/receive. |
| 2204 | */ |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2205 | unsigned int nand_subop_get_data_len(const struct nand_subop *subop, |
| 2206 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2207 | { |
| 2208 | int start_off = 0, end_off; |
| 2209 | |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2210 | if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2211 | !nand_instr_is_data(&subop->instrs[instr_idx]))) |
| 2212 | return 0; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2213 | |
| 2214 | start_off = nand_subop_get_data_start_off(subop, instr_idx); |
| 2215 | |
| 2216 | if (instr_idx == subop->ninstrs - 1 && |
| 2217 | subop->last_instr_end_off) |
| 2218 | end_off = subop->last_instr_end_off; |
| 2219 | else |
| 2220 | end_off = subop->instrs[instr_idx].ctx.data.len; |
| 2221 | |
| 2222 | return end_off - start_off; |
| 2223 | } |
| 2224 | EXPORT_SYMBOL_GPL(nand_subop_get_data_len); |
| 2225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | /** |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2227 | * nand_reset - Reset and initialize a NAND device |
| 2228 | * @chip: The NAND chip |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2229 | * @chipnr: Internal die id |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2230 | * |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 2231 | * Save the timings data structure, then apply SDR timings mode 0 (see |
| 2232 | * nand_reset_data_interface for details), do the reset operation, and |
| 2233 | * apply back the previous timings. |
| 2234 | * |
| 2235 | * Returns 0 on success, a negative error code otherwise. |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2236 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2237 | int nand_reset(struct nand_chip *chip, int chipnr) |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2238 | { |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 2239 | struct nand_data_interface saved_data_intf = chip->data_interface; |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 2240 | int ret; |
| 2241 | |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 2242 | ret = nand_reset_data_interface(chip, chipnr); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 2243 | if (ret) |
| 2244 | return ret; |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2245 | |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2246 | /* |
| 2247 | * The CS line has to be released before we can apply the new NAND |
| 2248 | * interface settings, hence this weird ->select_chip() dance. |
| 2249 | */ |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 2250 | chip->select_chip(chip, chipnr); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2251 | ret = nand_reset_op(chip); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 2252 | chip->select_chip(chip, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2253 | if (ret) |
| 2254 | return ret; |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2255 | |
Miquel Raynal | 107b7d6 | 2018-03-19 14:47:25 +0100 | [diff] [blame] | 2256 | /* |
| 2257 | * A nand_reset_data_interface() put both the NAND chip and the NAND |
| 2258 | * controller in timings mode 0. If the default mode for this chip is |
| 2259 | * also 0, no need to proceed to the change again. Plus, at probe time, |
| 2260 | * nand_setup_data_interface() uses ->set/get_features() which would |
| 2261 | * fail anyway as the parameter page is not available yet. |
| 2262 | */ |
| 2263 | if (!chip->onfi_timing_mode_default) |
| 2264 | return 0; |
| 2265 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 2266 | chip->data_interface = saved_data_intf; |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 2267 | ret = nand_setup_data_interface(chip, chipnr); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 2268 | if (ret) |
| 2269 | return ret; |
| 2270 | |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2271 | return 0; |
| 2272 | } |
Boris Brezillon | b9bb984 | 2017-10-05 18:53:19 +0200 | [diff] [blame] | 2273 | EXPORT_SYMBOL_GPL(nand_reset); |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2274 | |
| 2275 | /** |
Boris Brezillon | 4524036 | 2018-09-07 00:38:40 +0200 | [diff] [blame] | 2276 | * nand_get_features - wrapper to perform a GET_FEATURE |
| 2277 | * @chip: NAND chip info structure |
| 2278 | * @addr: feature address |
| 2279 | * @subfeature_param: the subfeature parameters, a four bytes array |
| 2280 | * |
| 2281 | * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the |
| 2282 | * operation cannot be handled. |
| 2283 | */ |
| 2284 | int nand_get_features(struct nand_chip *chip, int addr, |
| 2285 | u8 *subfeature_param) |
| 2286 | { |
| 2287 | if (!nand_supports_get_features(chip, addr)) |
| 2288 | return -ENOTSUPP; |
| 2289 | |
| 2290 | if (chip->legacy.get_features) |
| 2291 | return chip->legacy.get_features(chip, addr, subfeature_param); |
| 2292 | |
| 2293 | return nand_get_features_op(chip, addr, subfeature_param); |
| 2294 | } |
Boris Brezillon | 4524036 | 2018-09-07 00:38:40 +0200 | [diff] [blame] | 2295 | |
| 2296 | /** |
| 2297 | * nand_set_features - wrapper to perform a SET_FEATURE |
| 2298 | * @chip: NAND chip info structure |
| 2299 | * @addr: feature address |
| 2300 | * @subfeature_param: the subfeature parameters, a four bytes array |
| 2301 | * |
| 2302 | * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the |
| 2303 | * operation cannot be handled. |
| 2304 | */ |
| 2305 | int nand_set_features(struct nand_chip *chip, int addr, |
| 2306 | u8 *subfeature_param) |
| 2307 | { |
| 2308 | if (!nand_supports_set_features(chip, addr)) |
| 2309 | return -ENOTSUPP; |
| 2310 | |
| 2311 | if (chip->legacy.set_features) |
| 2312 | return chip->legacy.set_features(chip, addr, subfeature_param); |
| 2313 | |
| 2314 | return nand_set_features_op(chip, addr, subfeature_param); |
| 2315 | } |
Boris Brezillon | 4524036 | 2018-09-07 00:38:40 +0200 | [diff] [blame] | 2316 | |
| 2317 | /** |
Boris BREZILLON | 730a43f | 2015-09-03 18:03:38 +0200 | [diff] [blame] | 2318 | * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data |
| 2319 | * @buf: buffer to test |
| 2320 | * @len: buffer length |
| 2321 | * @bitflips_threshold: maximum number of bitflips |
| 2322 | * |
| 2323 | * Check if a buffer contains only 0xff, which means the underlying region |
| 2324 | * has been erased and is ready to be programmed. |
| 2325 | * The bitflips_threshold specify the maximum number of bitflips before |
| 2326 | * considering the region is not erased. |
| 2327 | * Note: The logic of this function has been extracted from the memweight |
| 2328 | * implementation, except that nand_check_erased_buf function exit before |
| 2329 | * testing the whole buffer if the number of bitflips exceed the |
| 2330 | * bitflips_threshold value. |
| 2331 | * |
| 2332 | * Returns a positive number of bitflips less than or equal to |
| 2333 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 2334 | * threshold. |
| 2335 | */ |
| 2336 | static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold) |
| 2337 | { |
| 2338 | const unsigned char *bitmap = buf; |
| 2339 | int bitflips = 0; |
| 2340 | int weight; |
| 2341 | |
| 2342 | for (; len && ((uintptr_t)bitmap) % sizeof(long); |
| 2343 | len--, bitmap++) { |
| 2344 | weight = hweight8(*bitmap); |
| 2345 | bitflips += BITS_PER_BYTE - weight; |
| 2346 | if (unlikely(bitflips > bitflips_threshold)) |
| 2347 | return -EBADMSG; |
| 2348 | } |
| 2349 | |
| 2350 | for (; len >= sizeof(long); |
| 2351 | len -= sizeof(long), bitmap += sizeof(long)) { |
Pavel Machek | 086567f | 2017-04-21 12:51:07 +0200 | [diff] [blame] | 2352 | unsigned long d = *((unsigned long *)bitmap); |
| 2353 | if (d == ~0UL) |
| 2354 | continue; |
| 2355 | weight = hweight_long(d); |
Boris BREZILLON | 730a43f | 2015-09-03 18:03:38 +0200 | [diff] [blame] | 2356 | bitflips += BITS_PER_LONG - weight; |
| 2357 | if (unlikely(bitflips > bitflips_threshold)) |
| 2358 | return -EBADMSG; |
| 2359 | } |
| 2360 | |
| 2361 | for (; len > 0; len--, bitmap++) { |
| 2362 | weight = hweight8(*bitmap); |
| 2363 | bitflips += BITS_PER_BYTE - weight; |
| 2364 | if (unlikely(bitflips > bitflips_threshold)) |
| 2365 | return -EBADMSG; |
| 2366 | } |
| 2367 | |
| 2368 | return bitflips; |
| 2369 | } |
| 2370 | |
| 2371 | /** |
| 2372 | * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only |
| 2373 | * 0xff data |
| 2374 | * @data: data buffer to test |
| 2375 | * @datalen: data length |
| 2376 | * @ecc: ECC buffer |
| 2377 | * @ecclen: ECC length |
| 2378 | * @extraoob: extra OOB buffer |
| 2379 | * @extraooblen: extra OOB length |
| 2380 | * @bitflips_threshold: maximum number of bitflips |
| 2381 | * |
| 2382 | * Check if a data buffer and its associated ECC and OOB data contains only |
| 2383 | * 0xff pattern, which means the underlying region has been erased and is |
| 2384 | * ready to be programmed. |
| 2385 | * The bitflips_threshold specify the maximum number of bitflips before |
| 2386 | * considering the region as not erased. |
| 2387 | * |
| 2388 | * Note: |
| 2389 | * 1/ ECC algorithms are working on pre-defined block sizes which are usually |
| 2390 | * different from the NAND page size. When fixing bitflips, ECC engines will |
| 2391 | * report the number of errors per chunk, and the NAND core infrastructure |
| 2392 | * expect you to return the maximum number of bitflips for the whole page. |
| 2393 | * This is why you should always use this function on a single chunk and |
| 2394 | * not on the whole page. After checking each chunk you should update your |
| 2395 | * max_bitflips value accordingly. |
| 2396 | * 2/ When checking for bitflips in erased pages you should not only check |
| 2397 | * the payload data but also their associated ECC data, because a user might |
| 2398 | * have programmed almost all bits to 1 but a few. In this case, we |
| 2399 | * shouldn't consider the chunk as erased, and checking ECC bytes prevent |
| 2400 | * this case. |
| 2401 | * 3/ The extraoob argument is optional, and should be used if some of your OOB |
| 2402 | * data are protected by the ECC engine. |
| 2403 | * It could also be used if you support subpages and want to attach some |
| 2404 | * extra OOB data to an ECC chunk. |
| 2405 | * |
| 2406 | * Returns a positive number of bitflips less than or equal to |
| 2407 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 2408 | * threshold. In case of success, the passed buffers are filled with 0xff. |
| 2409 | */ |
| 2410 | int nand_check_erased_ecc_chunk(void *data, int datalen, |
| 2411 | void *ecc, int ecclen, |
| 2412 | void *extraoob, int extraooblen, |
| 2413 | int bitflips_threshold) |
| 2414 | { |
| 2415 | int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0; |
| 2416 | |
| 2417 | data_bitflips = nand_check_erased_buf(data, datalen, |
| 2418 | bitflips_threshold); |
| 2419 | if (data_bitflips < 0) |
| 2420 | return data_bitflips; |
| 2421 | |
| 2422 | bitflips_threshold -= data_bitflips; |
| 2423 | |
| 2424 | ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold); |
| 2425 | if (ecc_bitflips < 0) |
| 2426 | return ecc_bitflips; |
| 2427 | |
| 2428 | bitflips_threshold -= ecc_bitflips; |
| 2429 | |
| 2430 | extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen, |
| 2431 | bitflips_threshold); |
| 2432 | if (extraoob_bitflips < 0) |
| 2433 | return extraoob_bitflips; |
| 2434 | |
| 2435 | if (data_bitflips) |
| 2436 | memset(data, 0xff, datalen); |
| 2437 | |
| 2438 | if (ecc_bitflips) |
| 2439 | memset(ecc, 0xff, ecclen); |
| 2440 | |
| 2441 | if (extraoob_bitflips) |
| 2442 | memset(extraoob, 0xff, extraooblen); |
| 2443 | |
| 2444 | return data_bitflips + ecc_bitflips + extraoob_bitflips; |
| 2445 | } |
| 2446 | EXPORT_SYMBOL(nand_check_erased_ecc_chunk); |
| 2447 | |
| 2448 | /** |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 2449 | * nand_read_page_raw_notsupp - dummy read raw page function |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 2450 | * @chip: nand chip info structure |
| 2451 | * @buf: buffer to store read data |
| 2452 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 2453 | * @page: page number to read |
| 2454 | * |
| 2455 | * Returns -ENOTSUPP unconditionally. |
| 2456 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2457 | int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf, |
| 2458 | int oob_required, int page) |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 2459 | { |
| 2460 | return -ENOTSUPP; |
| 2461 | } |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 2462 | |
| 2463 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2464 | * nand_read_page_raw - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2465 | * @chip: nand chip info structure |
| 2466 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2467 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2468 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2469 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2470 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2471 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2472 | int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required, |
| 2473 | int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2474 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2475 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2476 | int ret; |
| 2477 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2478 | ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2479 | if (ret) |
| 2480 | return ret; |
| 2481 | |
| 2482 | if (oob_required) { |
| 2483 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 2484 | false); |
| 2485 | if (ret) |
| 2486 | return ret; |
| 2487 | } |
| 2488 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2489 | return 0; |
| 2490 | } |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 2491 | EXPORT_SYMBOL(nand_read_page_raw); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2492 | |
| 2493 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2494 | * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2495 | * @chip: nand chip info structure |
| 2496 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2497 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2498 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2499 | * |
| 2500 | * We need a special oob layout and handling even when OOB isn't used. |
| 2501 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2502 | 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] | 2503 | int oob_required, int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2504 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2505 | struct mtd_info *mtd = nand_to_mtd(chip); |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2506 | int eccsize = chip->ecc.size; |
| 2507 | int eccbytes = chip->ecc.bytes; |
| 2508 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2509 | int steps, size, ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2510 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2511 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2512 | if (ret) |
| 2513 | return ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2514 | |
| 2515 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2516 | ret = nand_read_data_op(chip, buf, eccsize, false); |
| 2517 | if (ret) |
| 2518 | return ret; |
| 2519 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2520 | buf += eccsize; |
| 2521 | |
| 2522 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2523 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 2524 | false); |
| 2525 | if (ret) |
| 2526 | return ret; |
| 2527 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2528 | oob += chip->ecc.prepad; |
| 2529 | } |
| 2530 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2531 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 2532 | if (ret) |
| 2533 | return ret; |
| 2534 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2535 | oob += eccbytes; |
| 2536 | |
| 2537 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2538 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 2539 | false); |
| 2540 | if (ret) |
| 2541 | return ret; |
| 2542 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2543 | oob += chip->ecc.postpad; |
| 2544 | } |
| 2545 | } |
| 2546 | |
| 2547 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2548 | if (size) { |
| 2549 | ret = nand_read_data_op(chip, oob, size, false); |
| 2550 | if (ret) |
| 2551 | return ret; |
| 2552 | } |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2553 | |
| 2554 | return 0; |
| 2555 | } |
| 2556 | |
| 2557 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2558 | * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2559 | * @chip: nand chip info structure |
| 2560 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2561 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2562 | * @page: page number to read |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 2563 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2564 | static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf, |
| 2565 | int oob_required, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2567 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2568 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2569 | int eccbytes = chip->ecc.bytes; |
| 2570 | int eccsteps = chip->ecc.steps; |
| 2571 | uint8_t *p = buf; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2572 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
| 2573 | uint8_t *ecc_code = chip->ecc.code_buf; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2574 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2575 | |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2576 | chip->ecc.read_page_raw(chip, buf, 1, page); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2577 | |
| 2578 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 2579 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2580 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2581 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 2582 | chip->ecc.total); |
| 2583 | if (ret) |
| 2584 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2585 | |
| 2586 | eccsteps = chip->ecc.steps; |
| 2587 | p = buf; |
| 2588 | |
| 2589 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2590 | int stat; |
| 2591 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 2592 | stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]); |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2593 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2594 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2595 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2596 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2597 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2598 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2599 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2600 | return max_bitflips; |
Thomas Gleixner | 22c60f5 | 2005-04-04 19:56:32 +0100 | [diff] [blame] | 2601 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | /** |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2604 | * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2605 | * @chip: nand chip info structure |
| 2606 | * @data_offs: offset of requested data within the page |
| 2607 | * @readlen: data length |
| 2608 | * @bufpoi: buffer to store read data |
Huang Shijie | e004deb | 2014-01-03 11:01:40 +0800 | [diff] [blame] | 2609 | * @page: page number to read |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2610 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2611 | static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs, |
| 2612 | uint32_t readlen, uint8_t *bufpoi, int page) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2613 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2614 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2615 | int start_step, end_step, num_steps, ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2616 | uint8_t *p; |
| 2617 | int data_col_addr, i, gaps = 0; |
| 2618 | int datafrag_len, eccfrag_len, aligned_len, aligned_pos; |
| 2619 | int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2620 | int index, section = 0; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2621 | unsigned int max_bitflips = 0; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2622 | struct mtd_oob_region oobregion = { }; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2623 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2624 | /* Column address within the page aligned to ECC size (256bytes) */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2625 | start_step = data_offs / chip->ecc.size; |
| 2626 | end_step = (data_offs + readlen - 1) / chip->ecc.size; |
| 2627 | num_steps = end_step - start_step + 1; |
Ron | 4a4163ca | 2014-03-16 04:01:07 +1030 | [diff] [blame] | 2628 | index = start_step * chip->ecc.bytes; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2629 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2630 | /* Data size aligned to ECC ecc.size */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2631 | datafrag_len = num_steps * chip->ecc.size; |
| 2632 | eccfrag_len = num_steps * chip->ecc.bytes; |
| 2633 | |
| 2634 | data_col_addr = start_step * chip->ecc.size; |
| 2635 | /* If we read not a page aligned data */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2636 | p = bufpoi + data_col_addr; |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2637 | 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] | 2638 | if (ret) |
| 2639 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2640 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2641 | /* Calculate ECC */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2642 | 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] | 2643 | chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2644 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2645 | /* |
| 2646 | * The performance is faster if we position offsets according to |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2647 | * 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] | 2648 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2649 | ret = mtd_ooblayout_find_eccregion(mtd, index, §ion, &oobregion); |
| 2650 | if (ret) |
| 2651 | return ret; |
| 2652 | |
| 2653 | if (oobregion.length < eccfrag_len) |
| 2654 | gaps = 1; |
| 2655 | |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2656 | if (gaps) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2657 | ret = nand_change_read_column_op(chip, mtd->writesize, |
| 2658 | chip->oob_poi, mtd->oobsize, |
| 2659 | false); |
| 2660 | if (ret) |
| 2661 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2662 | } else { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2663 | /* |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2664 | * Send the command to read the particular ECC bytes take care |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2665 | * about buswidth alignment in read_buf. |
| 2666 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2667 | aligned_pos = oobregion.offset & ~(busw - 1); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2668 | aligned_len = eccfrag_len; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2669 | if (oobregion.offset & (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2670 | aligned_len++; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2671 | if ((oobregion.offset + (num_steps * chip->ecc.bytes)) & |
| 2672 | (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2673 | aligned_len++; |
| 2674 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2675 | ret = nand_change_read_column_op(chip, |
| 2676 | mtd->writesize + aligned_pos, |
| 2677 | &chip->oob_poi[aligned_pos], |
| 2678 | aligned_len, false); |
| 2679 | if (ret) |
| 2680 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2681 | } |
| 2682 | |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2683 | ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf, |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2684 | chip->oob_poi, index, eccfrag_len); |
| 2685 | if (ret) |
| 2686 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2687 | |
| 2688 | p = bufpoi + data_col_addr; |
| 2689 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { |
| 2690 | int stat; |
| 2691 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 2692 | stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i], |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2693 | &chip->ecc.calc_buf[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2694 | if (stat == -EBADMSG && |
| 2695 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2696 | /* check for empty pages with bitflips */ |
| 2697 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2698 | &chip->ecc.code_buf[i], |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2699 | chip->ecc.bytes, |
| 2700 | NULL, 0, |
| 2701 | chip->ecc.strength); |
| 2702 | } |
| 2703 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2704 | if (stat < 0) { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2705 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2706 | } else { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2707 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2708 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2709 | } |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2710 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2711 | return max_bitflips; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2712 | } |
| 2713 | |
| 2714 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2715 | * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2716 | * @chip: nand chip info structure |
| 2717 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2718 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2719 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2720 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2721 | * Not for syndrome calculating ECC controllers which need a special oob layout. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2722 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2723 | static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf, |
| 2724 | int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2725 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2726 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2727 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2728 | int eccbytes = chip->ecc.bytes; |
| 2729 | int eccsteps = chip->ecc.steps; |
| 2730 | uint8_t *p = buf; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2731 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
| 2732 | uint8_t *ecc_code = chip->ecc.code_buf; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2733 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2734 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2735 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2736 | if (ret) |
| 2737 | return ret; |
| 2738 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2739 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 2740 | chip->ecc.hwctl(chip, NAND_ECC_READ); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2741 | |
| 2742 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2743 | if (ret) |
| 2744 | return ret; |
| 2745 | |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 2746 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2747 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2748 | |
| 2749 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 2750 | if (ret) |
| 2751 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2752 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2753 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 2754 | chip->ecc.total); |
| 2755 | if (ret) |
| 2756 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2757 | |
| 2758 | eccsteps = chip->ecc.steps; |
| 2759 | p = buf; |
| 2760 | |
| 2761 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2762 | int stat; |
| 2763 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 2764 | stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2765 | if (stat == -EBADMSG && |
| 2766 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2767 | /* check for empty pages with bitflips */ |
| 2768 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 2769 | &ecc_code[i], eccbytes, |
| 2770 | NULL, 0, |
| 2771 | chip->ecc.strength); |
| 2772 | } |
| 2773 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2774 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2775 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2776 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2777 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2778 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2779 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2780 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2781 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2782 | } |
| 2783 | |
| 2784 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2785 | * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2786 | * @chip: nand chip info structure |
| 2787 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2788 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2789 | * @page: page number to read |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2790 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2791 | * Hardware ECC for large page chips, require OOB to be read first. For this |
| 2792 | * ECC mode, the write_page method is re-used from ECC_HW. These methods |
| 2793 | * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with |
| 2794 | * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from |
| 2795 | * the data area, by overwriting the NAND manufacturer bad block markings. |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2796 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2797 | static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf, |
| 2798 | int oob_required, int page) |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2799 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2800 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2801 | int i, eccsize = chip->ecc.size, ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2802 | int eccbytes = chip->ecc.bytes; |
| 2803 | int eccsteps = chip->ecc.steps; |
| 2804 | uint8_t *p = buf; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 2805 | uint8_t *ecc_code = chip->ecc.code_buf; |
| 2806 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2807 | unsigned int max_bitflips = 0; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2808 | |
| 2809 | /* Read the OOB area first */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2810 | ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); |
| 2811 | if (ret) |
| 2812 | return ret; |
| 2813 | |
| 2814 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2815 | if (ret) |
| 2816 | return ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2817 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2818 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 2819 | chip->ecc.total); |
| 2820 | if (ret) |
| 2821 | return ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2822 | |
| 2823 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2824 | int stat; |
| 2825 | |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 2826 | chip->ecc.hwctl(chip, NAND_ECC_READ); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2827 | |
| 2828 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2829 | if (ret) |
| 2830 | return ret; |
| 2831 | |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 2832 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2833 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 2834 | stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2835 | if (stat == -EBADMSG && |
| 2836 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2837 | /* check for empty pages with bitflips */ |
| 2838 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 2839 | &ecc_code[i], eccbytes, |
| 2840 | NULL, 0, |
| 2841 | chip->ecc.strength); |
| 2842 | } |
| 2843 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2844 | if (stat < 0) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2845 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2846 | } else { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2847 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2848 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2849 | } |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2850 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2851 | return max_bitflips; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 2852 | } |
| 2853 | |
| 2854 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2855 | * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2856 | * @chip: nand chip info structure |
| 2857 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2858 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2859 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2860 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2861 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 2862 | * need a special oob layout and handling. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2863 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2864 | static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf, |
| 2865 | int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2866 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2867 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2868 | int ret, i, eccsize = chip->ecc.size; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2869 | int eccbytes = chip->ecc.bytes; |
| 2870 | int eccsteps = chip->ecc.steps; |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2871 | int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2872 | uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2873 | uint8_t *oob = chip->oob_poi; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2874 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2875 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2876 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2877 | if (ret) |
| 2878 | return ret; |
| 2879 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2880 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2881 | int stat; |
| 2882 | |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 2883 | chip->ecc.hwctl(chip, NAND_ECC_READ); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2884 | |
| 2885 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 2886 | if (ret) |
| 2887 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2888 | |
| 2889 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2890 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 2891 | false); |
| 2892 | if (ret) |
| 2893 | return ret; |
| 2894 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2895 | oob += chip->ecc.prepad; |
| 2896 | } |
| 2897 | |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 2898 | chip->ecc.hwctl(chip, NAND_ECC_READSYN); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2899 | |
| 2900 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 2901 | if (ret) |
| 2902 | return ret; |
| 2903 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 2904 | stat = chip->ecc.correct(chip, p, oob, NULL); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2905 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2906 | oob += eccbytes; |
| 2907 | |
| 2908 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2909 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 2910 | false); |
| 2911 | if (ret) |
| 2912 | return ret; |
| 2913 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2914 | oob += chip->ecc.postpad; |
| 2915 | } |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 2916 | |
| 2917 | if (stat == -EBADMSG && |
| 2918 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 2919 | /* check for empty pages with bitflips */ |
| 2920 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 2921 | oob - eccpadbytes, |
| 2922 | eccpadbytes, |
| 2923 | NULL, 0, |
| 2924 | chip->ecc.strength); |
| 2925 | } |
| 2926 | |
| 2927 | if (stat < 0) { |
| 2928 | mtd->ecc_stats.failed++; |
| 2929 | } else { |
| 2930 | mtd->ecc_stats.corrected += stat; |
| 2931 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 2932 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2933 | } |
| 2934 | |
| 2935 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 2936 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2937 | if (i) { |
| 2938 | ret = nand_read_data_op(chip, oob, i, false); |
| 2939 | if (ret) |
| 2940 | return ret; |
| 2941 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2942 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2943 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2944 | } |
| 2945 | |
| 2946 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2947 | * nand_transfer_oob - [INTERN] Transfer oob to client buffer |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 2948 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2949 | * @oob: oob destination address |
| 2950 | * @ops: oob ops structure |
| 2951 | * @len: size of oob to transfer |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2952 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 2953 | static uint8_t *nand_transfer_oob(struct nand_chip *chip, uint8_t *oob, |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2954 | struct mtd_oob_ops *ops, size_t len) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2955 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 2956 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2957 | int ret; |
| 2958 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2959 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2960 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2961 | case MTD_OPS_PLACE_OOB: |
| 2962 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2963 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); |
| 2964 | return oob + len; |
| 2965 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2966 | case MTD_OPS_AUTO_OOB: |
| 2967 | ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi, |
| 2968 | ops->ooboffs, len); |
| 2969 | BUG_ON(ret); |
| 2970 | return oob + len; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2971 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2972 | default: |
| 2973 | BUG(); |
| 2974 | } |
| 2975 | return NULL; |
| 2976 | } |
| 2977 | |
| 2978 | /** |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2979 | * nand_setup_read_retry - [INTERN] Set the READ RETRY mode |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 2980 | * @chip: NAND chip object |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2981 | * @retry_mode: the retry mode to use |
| 2982 | * |
| 2983 | * Some vendors supply a special command to shift the Vt threshold, to be used |
| 2984 | * when there are too many bitflips in a page (i.e., ECC error). After setting |
| 2985 | * a new threshold, the host should retry reading the page. |
| 2986 | */ |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 2987 | 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] | 2988 | { |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2989 | pr_debug("setting READ RETRY mode %d\n", retry_mode); |
| 2990 | |
| 2991 | if (retry_mode >= chip->read_retries) |
| 2992 | return -EINVAL; |
| 2993 | |
| 2994 | if (!chip->setup_read_retry) |
| 2995 | return -EOPNOTSUPP; |
| 2996 | |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 2997 | return chip->setup_read_retry(chip, retry_mode); |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2998 | } |
| 2999 | |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3000 | static void nand_wait_readrdy(struct nand_chip *chip) |
| 3001 | { |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 3002 | const struct nand_sdr_timings *sdr; |
| 3003 | |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3004 | if (!(chip->options & NAND_NEED_READRDY)) |
| 3005 | return; |
| 3006 | |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 3007 | sdr = nand_get_sdr_timings(&chip->data_interface); |
| 3008 | 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] | 3009 | } |
| 3010 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3011 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3012 | * nand_do_read_ops - [INTERN] Read data with ECC |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3013 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3014 | * @from: offset to read from |
| 3015 | * @ops: oob ops structure |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 3016 | * |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3017 | * Internal function. Called with chip held. |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 3018 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3019 | static int nand_do_read_ops(struct nand_chip *chip, loff_t from, |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3020 | struct mtd_oob_ops *ops) |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 3021 | { |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3022 | int chipnr, page, realpage, col, bytes, aligned, oob_required; |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3023 | struct mtd_info *mtd = nand_to_mtd(chip); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3024 | int ret = 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3025 | uint32_t readlen = ops->len; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3026 | uint32_t oobreadlen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 3027 | uint32_t max_oobsize = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 9aca334 | 2010-02-22 20:39:35 +0200 | [diff] [blame] | 3028 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3029 | uint8_t *bufpoi, *oob, *buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3030 | int use_bufpoi; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3031 | unsigned int max_bitflips = 0; |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3032 | int retry_mode = 0; |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3033 | bool ecc_fail = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3034 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3035 | chipnr = (int)(from >> chip->chip_shift); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3036 | chip->select_chip(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3037 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3038 | realpage = (int)(from >> chip->page_shift); |
| 3039 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3040 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3041 | col = (int)(from & (mtd->writesize - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3042 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3043 | buf = ops->datbuf; |
| 3044 | oob = ops->oobbuf; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3045 | oob_required = oob ? 1 : 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3046 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3047 | while (1) { |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3048 | unsigned int ecc_failures = mtd->ecc_stats.failed; |
| 3049 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3050 | bytes = min(mtd->writesize - col, readlen); |
| 3051 | aligned = (bytes == mtd->writesize); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3052 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3053 | if (!aligned) |
| 3054 | use_bufpoi = 1; |
| 3055 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 3056 | use_bufpoi = !virt_addr_valid(buf) || |
| 3057 | !IS_ALIGNED((unsigned long)buf, |
| 3058 | chip->buf_align); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3059 | else |
| 3060 | use_bufpoi = 0; |
| 3061 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3062 | /* Is the current page in the buffer? */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3063 | if (realpage != chip->pagebuf || oob) { |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3064 | bufpoi = use_bufpoi ? chip->data_buf : buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3065 | |
| 3066 | if (use_bufpoi && aligned) |
| 3067 | pr_debug("%s: using read bounce buffer for buf@%p\n", |
| 3068 | __func__, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3069 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3070 | read_retry: |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3071 | /* |
| 3072 | * Now read the page into the buffer. Absent an error, |
| 3073 | * the read methods return max bitflips per ecc step. |
| 3074 | */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3075 | if (unlikely(ops->mode == MTD_OPS_RAW)) |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3076 | ret = chip->ecc.read_page_raw(chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3077 | oob_required, |
| 3078 | page); |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 3079 | else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) && |
| 3080 | !oob) |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3081 | ret = chip->ecc.read_subpage(chip, col, bytes, |
| 3082 | bufpoi, page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 3083 | else |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3084 | ret = chip->ecc.read_page(chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3085 | oob_required, page); |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3086 | if (ret < 0) { |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3087 | if (use_bufpoi) |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3088 | /* Invalidate page cache */ |
| 3089 | chip->pagebuf = -1; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3090 | break; |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3091 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3092 | |
| 3093 | /* Transfer not aligned data */ |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3094 | if (use_bufpoi) { |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 3095 | if (!NAND_HAS_SUBPAGE_READ(chip) && !oob && |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3096 | !(mtd->ecc_stats.failed - ecc_failures) && |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3097 | (ops->mode != MTD_OPS_RAW)) { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3098 | chip->pagebuf = realpage; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3099 | chip->pagebuf_bitflips = ret; |
| 3100 | } else { |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3101 | /* Invalidate page cache */ |
| 3102 | chip->pagebuf = -1; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3103 | } |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3104 | memcpy(buf, chip->data_buf + col, bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3105 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3106 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3107 | if (unlikely(oob)) { |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 3108 | int toread = min(oobreadlen, max_oobsize); |
| 3109 | |
| 3110 | if (toread) { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3111 | oob = nand_transfer_oob(chip, oob, ops, |
| 3112 | toread); |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 3113 | oobreadlen -= toread; |
| 3114 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3115 | } |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 3116 | |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3117 | nand_wait_readrdy(chip); |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3118 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3119 | if (mtd->ecc_stats.failed - ecc_failures) { |
Brian Norris | 28fa65e | 2014-02-12 16:08:28 -0800 | [diff] [blame] | 3120 | if (retry_mode + 1 < chip->read_retries) { |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3121 | retry_mode++; |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 3122 | ret = nand_setup_read_retry(chip, |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3123 | retry_mode); |
| 3124 | if (ret < 0) |
| 3125 | break; |
| 3126 | |
| 3127 | /* Reset failures; retry */ |
| 3128 | mtd->ecc_stats.failed = ecc_failures; |
| 3129 | goto read_retry; |
| 3130 | } else { |
| 3131 | /* No more retry modes; real failure */ |
| 3132 | ecc_fail = true; |
| 3133 | } |
| 3134 | } |
| 3135 | |
| 3136 | buf += bytes; |
Masahiro Yamada | 0760468 | 2017-03-30 15:45:47 +0900 | [diff] [blame] | 3137 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3138 | } else { |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3139 | memcpy(buf, chip->data_buf + col, bytes); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3140 | buf += bytes; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3141 | max_bitflips = max_t(unsigned int, max_bitflips, |
| 3142 | chip->pagebuf_bitflips); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3143 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3145 | readlen -= bytes; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3146 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3147 | /* Reset to retry mode 0 */ |
| 3148 | if (retry_mode) { |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 3149 | ret = nand_setup_read_retry(chip, 0); |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3150 | if (ret < 0) |
| 3151 | break; |
| 3152 | retry_mode = 0; |
| 3153 | } |
| 3154 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3155 | if (!readlen) |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3156 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3157 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3158 | /* For subsequent reads align to page boundary */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | col = 0; |
| 3160 | /* Increment page address */ |
| 3161 | realpage++; |
| 3162 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3163 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | /* Check, if we cross a chip boundary */ |
| 3165 | if (!page) { |
| 3166 | chipnr++; |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3167 | chip->select_chip(chip, -1); |
| 3168 | chip->select_chip(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3170 | } |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3171 | chip->select_chip(chip, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3173 | ops->retlen = ops->len - (size_t) readlen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3174 | if (oob) |
| 3175 | ops->oobretlen = ops->ooblen - oobreadlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3176 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3177 | if (ret < 0) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3178 | return ret; |
| 3179 | |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3180 | if (ecc_fail) |
Thomas Gleixner | 9a1fcdf | 2006-05-29 14:56:39 +0200 | [diff] [blame] | 3181 | return -EBADMSG; |
| 3182 | |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3183 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3184 | } |
| 3185 | |
| 3186 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3187 | * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3188 | * @chip: nand chip info structure |
| 3189 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3190 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3191 | int nand_read_oob_std(struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3192 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3193 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 3194 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3195 | 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] | 3196 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 3197 | EXPORT_SYMBOL(nand_read_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3198 | |
| 3199 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3200 | * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3201 | * with syndromes |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3202 | * @chip: nand chip info structure |
| 3203 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3204 | */ |
Boris Brezillon | 348d56a | 2018-09-07 00:38:48 +0200 | [diff] [blame] | 3205 | static int nand_read_oob_syndrome(struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3206 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3207 | struct mtd_info *mtd = nand_to_mtd(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3208 | int length = mtd->oobsize; |
| 3209 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 3210 | int eccsize = chip->ecc.size; |
Baruch Siach | 2ea69d2 | 2015-01-22 15:23:05 +0200 | [diff] [blame] | 3211 | uint8_t *bufpoi = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3212 | int i, toread, sndrnd = 0, pos, ret; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3213 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3214 | ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0); |
| 3215 | if (ret) |
| 3216 | return ret; |
| 3217 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3218 | for (i = 0; i < chip->ecc.steps; i++) { |
| 3219 | if (sndrnd) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3220 | int ret; |
| 3221 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3222 | pos = eccsize + i * (eccsize + chunk); |
| 3223 | if (mtd->writesize > 512) |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3224 | ret = nand_change_read_column_op(chip, pos, |
| 3225 | NULL, 0, |
| 3226 | false); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3227 | else |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3228 | ret = nand_read_page_op(chip, page, pos, NULL, |
| 3229 | 0); |
| 3230 | |
| 3231 | if (ret) |
| 3232 | return ret; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3233 | } else |
| 3234 | sndrnd = 1; |
| 3235 | toread = min_t(int, length, chunk); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3236 | |
| 3237 | ret = nand_read_data_op(chip, bufpoi, toread, false); |
| 3238 | if (ret) |
| 3239 | return ret; |
| 3240 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3241 | bufpoi += toread; |
| 3242 | length -= toread; |
| 3243 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3244 | if (length > 0) { |
| 3245 | ret = nand_read_data_op(chip, bufpoi, length, false); |
| 3246 | if (ret) |
| 3247 | return ret; |
| 3248 | } |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3249 | |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 3250 | return 0; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3251 | } |
| 3252 | |
| 3253 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3254 | * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3255 | * @chip: nand chip info structure |
| 3256 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3257 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3258 | int nand_write_oob_std(struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3259 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3260 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 3261 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3262 | return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi, |
| 3263 | mtd->oobsize); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3264 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 3265 | EXPORT_SYMBOL(nand_write_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3266 | |
| 3267 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3268 | * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3269 | * with syndrome - only for large page flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3270 | * @chip: nand chip info structure |
| 3271 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3272 | */ |
Boris Brezillon | 348d56a | 2018-09-07 00:38:48 +0200 | [diff] [blame] | 3273 | static int nand_write_oob_syndrome(struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3274 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3275 | struct mtd_info *mtd = nand_to_mtd(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3276 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 3277 | int eccsize = chip->ecc.size, length = mtd->oobsize; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3278 | int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3279 | const uint8_t *bufpoi = chip->oob_poi; |
| 3280 | |
| 3281 | /* |
| 3282 | * data-ecc-data-ecc ... ecc-oob |
| 3283 | * or |
| 3284 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob |
| 3285 | */ |
| 3286 | if (!chip->ecc.prepad && !chip->ecc.postpad) { |
| 3287 | pos = steps * (eccsize + chunk); |
| 3288 | steps = 0; |
| 3289 | } else |
Vitaly Wool | 8b0036e | 2006-07-11 09:11:25 +0200 | [diff] [blame] | 3290 | pos = eccsize; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3291 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3292 | ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0); |
| 3293 | if (ret) |
| 3294 | return ret; |
| 3295 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3296 | for (i = 0; i < steps; i++) { |
| 3297 | if (sndcmd) { |
| 3298 | if (mtd->writesize <= 512) { |
| 3299 | uint32_t fill = 0xFFFFFFFF; |
| 3300 | |
| 3301 | len = eccsize; |
| 3302 | while (len > 0) { |
| 3303 | int num = min_t(int, len, 4); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3304 | |
| 3305 | ret = nand_write_data_op(chip, &fill, |
| 3306 | num, false); |
| 3307 | if (ret) |
| 3308 | return ret; |
| 3309 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3310 | len -= num; |
| 3311 | } |
| 3312 | } else { |
| 3313 | pos = eccsize + i * (eccsize + chunk); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3314 | ret = nand_change_write_column_op(chip, pos, |
| 3315 | NULL, 0, |
| 3316 | false); |
| 3317 | if (ret) |
| 3318 | return ret; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3319 | } |
| 3320 | } else |
| 3321 | sndcmd = 1; |
| 3322 | len = min_t(int, length, chunk); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3323 | |
| 3324 | ret = nand_write_data_op(chip, bufpoi, len, false); |
| 3325 | if (ret) |
| 3326 | return ret; |
| 3327 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3328 | bufpoi += len; |
| 3329 | length -= len; |
| 3330 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3331 | if (length > 0) { |
| 3332 | ret = nand_write_data_op(chip, bufpoi, length, false); |
| 3333 | if (ret) |
| 3334 | return ret; |
| 3335 | } |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3336 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3337 | return nand_prog_page_end_op(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3338 | } |
| 3339 | |
| 3340 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3341 | * nand_do_read_oob - [INTERN] NAND read out-of-band |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3342 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3343 | * @from: offset to read from |
| 3344 | * @ops: oob operations description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3345 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3346 | * NAND read out-of-band data from the spare area. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3347 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3348 | static int nand_do_read_oob(struct nand_chip *chip, loff_t from, |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3349 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3350 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3351 | struct mtd_info *mtd = nand_to_mtd(chip); |
Miquel Raynal | 87e89ce | 2018-01-12 10:13:36 +0100 | [diff] [blame] | 3352 | unsigned int max_bitflips = 0; |
Brian Norris | c00a099 | 2012-05-01 17:12:54 -0700 | [diff] [blame] | 3353 | int page, realpage, chipnr; |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 3354 | struct mtd_ecc_stats stats; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3355 | int readlen = ops->ooblen; |
| 3356 | int len; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3357 | uint8_t *buf = ops->oobbuf; |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 3358 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3359 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3360 | pr_debug("%s: from = 0x%08Lx, len = %i\n", |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 3361 | __func__, (unsigned long long)from, readlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3362 | |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 3363 | stats = mtd->ecc_stats; |
| 3364 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 3365 | len = mtd_oobavail(mtd, ops); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 3366 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3367 | chipnr = (int)(from >> chip->chip_shift); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3368 | chip->select_chip(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3369 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3370 | /* Shift to get page */ |
| 3371 | realpage = (int)(from >> chip->page_shift); |
| 3372 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3373 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3374 | while (1) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3375 | if (ops->mode == MTD_OPS_RAW) |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3376 | ret = chip->ecc.read_oob_raw(chip, page); |
Brian Norris | c46f648 | 2011-08-30 18:45:38 -0700 | [diff] [blame] | 3377 | else |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3378 | ret = chip->ecc.read_oob(chip, page); |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 3379 | |
| 3380 | if (ret < 0) |
| 3381 | break; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3382 | |
| 3383 | len = min(len, readlen); |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3384 | buf = nand_transfer_oob(chip, buf, ops, len); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3385 | |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3386 | nand_wait_readrdy(chip); |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 3387 | |
Miquel Raynal | 87e89ce | 2018-01-12 10:13:36 +0100 | [diff] [blame] | 3388 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
| 3389 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3390 | readlen -= len; |
Savin Zlobec | 0d420f9 | 2006-06-21 11:51:20 +0200 | [diff] [blame] | 3391 | if (!readlen) |
| 3392 | break; |
| 3393 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3394 | /* Increment page address */ |
| 3395 | realpage++; |
| 3396 | |
| 3397 | page = realpage & chip->pagemask; |
| 3398 | /* Check, if we cross a chip boundary */ |
| 3399 | if (!page) { |
| 3400 | chipnr++; |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3401 | chip->select_chip(chip, -1); |
| 3402 | chip->select_chip(chip, chipnr); |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3403 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3404 | } |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3405 | chip->select_chip(chip, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3406 | |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 3407 | ops->oobretlen = ops->ooblen - readlen; |
| 3408 | |
| 3409 | if (ret < 0) |
| 3410 | return ret; |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 3411 | |
| 3412 | if (mtd->ecc_stats.failed - stats.failed) |
| 3413 | return -EBADMSG; |
| 3414 | |
Miquel Raynal | 87e89ce | 2018-01-12 10:13:36 +0100 | [diff] [blame] | 3415 | return max_bitflips; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3416 | } |
| 3417 | |
| 3418 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3419 | * 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] | 3420 | * @mtd: MTD device structure |
| 3421 | * @from: offset to read from |
| 3422 | * @ops: oob operation description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3423 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3424 | * NAND read data and/or out-of-band data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3425 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3426 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
| 3427 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3428 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3429 | struct nand_chip *chip = mtd_to_nand(mtd); |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 3430 | int ret; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3431 | |
| 3432 | ops->retlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3433 | |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 3434 | if (ops->mode != MTD_OPS_PLACE_OOB && |
| 3435 | ops->mode != MTD_OPS_AUTO_OOB && |
| 3436 | ops->mode != MTD_OPS_RAW) |
| 3437 | return -ENOTSUPP; |
| 3438 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3439 | nand_get_device(chip, FL_READING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3440 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3441 | if (!ops->datbuf) |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3442 | ret = nand_do_read_oob(chip, from, ops); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3443 | else |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3444 | ret = nand_do_read_ops(chip, from, ops); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3445 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3446 | nand_release_device(chip); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3447 | return ret; |
| 3448 | } |
| 3449 | |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 3450 | /** |
| 3451 | * nand_write_page_raw_notsupp - dummy raw page write function |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 3452 | * @chip: nand chip info structure |
| 3453 | * @buf: data buffer |
| 3454 | * @oob_required: must write chip->oob_poi to OOB |
| 3455 | * @page: page number to write |
| 3456 | * |
| 3457 | * Returns -ENOTSUPP unconditionally. |
| 3458 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3459 | int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf, |
| 3460 | int oob_required, int page) |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 3461 | { |
| 3462 | return -ENOTSUPP; |
| 3463 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3464 | |
| 3465 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3466 | * nand_write_page_raw - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3467 | * @chip: nand chip info structure |
| 3468 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3469 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3470 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3471 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3472 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3473 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3474 | int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf, |
| 3475 | int oob_required, int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3476 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3477 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3478 | int ret; |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3479 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3480 | ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3481 | if (ret) |
| 3482 | return ret; |
| 3483 | |
| 3484 | if (oob_required) { |
| 3485 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 3486 | false); |
| 3487 | if (ret) |
| 3488 | return ret; |
| 3489 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3490 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3491 | return nand_prog_page_end_op(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3492 | } |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 3493 | EXPORT_SYMBOL(nand_write_page_raw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3494 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3495 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3496 | * nand_write_page_raw_syndrome - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3497 | * @chip: nand chip info structure |
| 3498 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3499 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3500 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3501 | * |
| 3502 | * We need a special oob layout and handling even when ECC isn't checked. |
| 3503 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3504 | static int nand_write_page_raw_syndrome(struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3505 | const uint8_t *buf, int oob_required, |
| 3506 | int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3507 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3508 | struct mtd_info *mtd = nand_to_mtd(chip); |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3509 | int eccsize = chip->ecc.size; |
| 3510 | int eccbytes = chip->ecc.bytes; |
| 3511 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3512 | int steps, size, ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3513 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3514 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3515 | if (ret) |
| 3516 | return ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3517 | |
| 3518 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3519 | ret = nand_write_data_op(chip, buf, eccsize, false); |
| 3520 | if (ret) |
| 3521 | return ret; |
| 3522 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3523 | buf += eccsize; |
| 3524 | |
| 3525 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3526 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 3527 | false); |
| 3528 | if (ret) |
| 3529 | return ret; |
| 3530 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3531 | oob += chip->ecc.prepad; |
| 3532 | } |
| 3533 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3534 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 3535 | if (ret) |
| 3536 | return ret; |
| 3537 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3538 | oob += eccbytes; |
| 3539 | |
| 3540 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3541 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 3542 | false); |
| 3543 | if (ret) |
| 3544 | return ret; |
| 3545 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3546 | oob += chip->ecc.postpad; |
| 3547 | } |
| 3548 | } |
| 3549 | |
| 3550 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3551 | if (size) { |
| 3552 | ret = nand_write_data_op(chip, oob, size, false); |
| 3553 | if (ret) |
| 3554 | return ret; |
| 3555 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3556 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3557 | return nand_prog_page_end_op(chip); |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3558 | } |
| 3559 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3560 | * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3561 | * @chip: nand chip info structure |
| 3562 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3563 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3564 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3565 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3566 | static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf, |
| 3567 | int oob_required, int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3568 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3569 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3570 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3571 | int eccbytes = chip->ecc.bytes; |
| 3572 | int eccsteps = chip->ecc.steps; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3573 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3574 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3575 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3576 | /* Software ECC calculation */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3577 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 3578 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3579 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3580 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 3581 | chip->ecc.total); |
| 3582 | if (ret) |
| 3583 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3584 | |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3585 | return chip->ecc.write_page_raw(chip, buf, 1, page); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3586 | } |
| 3587 | |
| 3588 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3589 | * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3590 | * @chip: nand chip info structure |
| 3591 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3592 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3593 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3594 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3595 | static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf, |
| 3596 | int oob_required, int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3597 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3598 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3599 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3600 | int eccbytes = chip->ecc.bytes; |
| 3601 | int eccsteps = chip->ecc.steps; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3602 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3603 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3604 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3605 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3606 | if (ret) |
| 3607 | return ret; |
| 3608 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3609 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 3610 | chip->ecc.hwctl(chip, NAND_ECC_WRITE); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3611 | |
| 3612 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 3613 | if (ret) |
| 3614 | return ret; |
| 3615 | |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 3616 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3617 | } |
| 3618 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3619 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 3620 | chip->ecc.total); |
| 3621 | if (ret) |
| 3622 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3623 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3624 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 3625 | if (ret) |
| 3626 | return ret; |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3627 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3628 | return nand_prog_page_end_op(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3629 | } |
| 3630 | |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3631 | |
| 3632 | /** |
Brian Norris | 73c8aaf | 2015-02-28 02:04:18 -0800 | [diff] [blame] | 3633 | * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3634 | * @chip: nand chip info structure |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 3635 | * @offset: column address of subpage within the page |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3636 | * @data_len: data length |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 3637 | * @buf: data buffer |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3638 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3639 | * @page: page number to write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3640 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3641 | static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset, |
| 3642 | uint32_t data_len, const uint8_t *buf, |
| 3643 | int oob_required, int page) |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3644 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3645 | struct mtd_info *mtd = nand_to_mtd(chip); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3646 | uint8_t *oob_buf = chip->oob_poi; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3647 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3648 | int ecc_size = chip->ecc.size; |
| 3649 | int ecc_bytes = chip->ecc.bytes; |
| 3650 | int ecc_steps = chip->ecc.steps; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3651 | uint32_t start_step = offset / ecc_size; |
| 3652 | uint32_t end_step = (offset + data_len - 1) / ecc_size; |
| 3653 | int oob_bytes = mtd->oobsize / ecc_steps; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3654 | int step, ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3655 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3656 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3657 | if (ret) |
| 3658 | return ret; |
| 3659 | |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3660 | for (step = 0; step < ecc_steps; step++) { |
| 3661 | /* configure controller for WRITE access */ |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 3662 | chip->ecc.hwctl(chip, NAND_ECC_WRITE); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3663 | |
| 3664 | /* write data (untouched subpages already masked by 0xFF) */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3665 | ret = nand_write_data_op(chip, buf, ecc_size, false); |
| 3666 | if (ret) |
| 3667 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3668 | |
| 3669 | /* mask ECC of un-touched subpages by padding 0xFF */ |
| 3670 | if ((step < start_step) || (step > end_step)) |
| 3671 | memset(ecc_calc, 0xff, ecc_bytes); |
| 3672 | else |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 3673 | chip->ecc.calculate(chip, buf, ecc_calc); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3674 | |
| 3675 | /* mask OOB of un-touched subpages by padding 0xFF */ |
| 3676 | /* if oob_required, preserve OOB metadata of written subpage */ |
| 3677 | if (!oob_required || (step < start_step) || (step > end_step)) |
| 3678 | memset(oob_buf, 0xff, oob_bytes); |
| 3679 | |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 3680 | buf += ecc_size; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3681 | ecc_calc += ecc_bytes; |
| 3682 | oob_buf += oob_bytes; |
| 3683 | } |
| 3684 | |
| 3685 | /* copy calculated ECC for whole page to chip->buffer->oob */ |
| 3686 | /* this include masked-value(0xFF) for unwritten subpages */ |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3687 | ecc_calc = chip->ecc.calc_buf; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3688 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 3689 | chip->ecc.total); |
| 3690 | if (ret) |
| 3691 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3692 | |
| 3693 | /* write OOB buffer to NAND device */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3694 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 3695 | if (ret) |
| 3696 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3697 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3698 | return nand_prog_page_end_op(chip); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3699 | } |
| 3700 | |
| 3701 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3702 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3703 | * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3704 | * @chip: nand chip info structure |
| 3705 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3706 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3707 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3708 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3709 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 3710 | * need a special oob layout and handling. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3711 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3712 | static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf, |
| 3713 | int oob_required, int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3714 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3715 | struct mtd_info *mtd = nand_to_mtd(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3716 | int i, eccsize = chip->ecc.size; |
| 3717 | int eccbytes = chip->ecc.bytes; |
| 3718 | int eccsteps = chip->ecc.steps; |
| 3719 | const uint8_t *p = buf; |
| 3720 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3721 | int ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3722 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3723 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3724 | if (ret) |
| 3725 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3726 | |
| 3727 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 3728 | chip->ecc.hwctl(chip, NAND_ECC_WRITE); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3729 | |
| 3730 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 3731 | if (ret) |
| 3732 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3733 | |
| 3734 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3735 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 3736 | false); |
| 3737 | if (ret) |
| 3738 | return ret; |
| 3739 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3740 | oob += chip->ecc.prepad; |
| 3741 | } |
| 3742 | |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 3743 | chip->ecc.calculate(chip, p, oob); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3744 | |
| 3745 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 3746 | if (ret) |
| 3747 | return ret; |
| 3748 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3749 | oob += eccbytes; |
| 3750 | |
| 3751 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3752 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 3753 | false); |
| 3754 | if (ret) |
| 3755 | return ret; |
| 3756 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3757 | oob += chip->ecc.postpad; |
| 3758 | } |
| 3759 | } |
| 3760 | |
| 3761 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 3762 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3763 | if (i) { |
| 3764 | ret = nand_write_data_op(chip, oob, i, false); |
| 3765 | if (ret) |
| 3766 | return ret; |
| 3767 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3768 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3769 | return nand_prog_page_end_op(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3770 | } |
| 3771 | |
| 3772 | /** |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 3773 | * nand_write_page - write one page |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3774 | * @chip: NAND chip descriptor |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3775 | * @offset: address offset within the page |
| 3776 | * @data_len: length of actual data to be written |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3777 | * @buf: the data to write |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3778 | * @oob_required: must write chip->oob_poi to OOB |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3779 | * @page: page number to write |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3780 | * @raw: use _raw version of write_page |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3781 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3782 | static int nand_write_page(struct nand_chip *chip, uint32_t offset, |
| 3783 | int data_len, const uint8_t *buf, int oob_required, |
| 3784 | int page, int raw) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3785 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3786 | struct mtd_info *mtd = nand_to_mtd(chip); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3787 | int status, subpage; |
| 3788 | |
| 3789 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && |
| 3790 | chip->ecc.write_subpage) |
| 3791 | subpage = offset || (data_len < mtd->writesize); |
| 3792 | else |
| 3793 | subpage = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3794 | |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 3795 | if (unlikely(raw)) |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3796 | status = chip->ecc.write_page_raw(chip, buf, oob_required, |
| 3797 | page); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3798 | else if (subpage) |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3799 | status = chip->ecc.write_subpage(chip, offset, data_len, buf, |
| 3800 | oob_required, page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 3801 | else |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3802 | status = chip->ecc.write_page(chip, buf, oob_required, page); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3803 | |
| 3804 | if (status < 0) |
| 3805 | return status; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3806 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3807 | return 0; |
| 3808 | } |
| 3809 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3810 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3811 | * nand_fill_oob - [INTERN] Transfer client buffer to oob |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3812 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3813 | * @oob: oob data buffer |
| 3814 | * @len: oob data write length |
| 3815 | * @ops: oob ops structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3816 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3817 | static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, size_t len, |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 3818 | struct mtd_oob_ops *ops) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3819 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3820 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3821 | int ret; |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 3822 | |
| 3823 | /* |
| 3824 | * Initialise to all 0xFF, to avoid the possibility of left over OOB |
| 3825 | * data from a previous OOB read. |
| 3826 | */ |
| 3827 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
| 3828 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3829 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3830 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3831 | case MTD_OPS_PLACE_OOB: |
| 3832 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3833 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); |
| 3834 | return oob + len; |
| 3835 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3836 | case MTD_OPS_AUTO_OOB: |
| 3837 | ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi, |
| 3838 | ops->ooboffs, len); |
| 3839 | BUG_ON(ret); |
| 3840 | return oob + len; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3841 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3842 | default: |
| 3843 | BUG(); |
| 3844 | } |
| 3845 | return NULL; |
| 3846 | } |
| 3847 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3848 | #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3849 | |
| 3850 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3851 | * nand_do_write_ops - [INTERN] NAND write with ECC |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3852 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3853 | * @to: offset to write to |
| 3854 | * @ops: oob operations description structure |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3855 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3856 | * NAND write with ECC. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3857 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3858 | static int nand_do_write_ops(struct nand_chip *chip, loff_t to, |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3859 | struct mtd_oob_ops *ops) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3860 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3861 | struct mtd_info *mtd = nand_to_mtd(chip); |
Corentin Labbe | 73600b6 | 2017-09-02 10:49:38 +0200 | [diff] [blame] | 3862 | int chipnr, realpage, page, column; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3863 | uint32_t writelen = ops->len; |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3864 | |
| 3865 | uint32_t oobwritelen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 3866 | uint32_t oobmaxlen = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3867 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3868 | uint8_t *oob = ops->oobbuf; |
| 3869 | uint8_t *buf = ops->datbuf; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3870 | int ret; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3871 | int oob_required = oob ? 1 : 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3872 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3873 | ops->retlen = 0; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3874 | if (!writelen) |
| 3875 | return 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3876 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3877 | /* Reject writes, which are not page aligned */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3878 | if (NOTALIGNED(to) || NOTALIGNED(ops->len)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 3879 | pr_notice("%s: attempt to write non page aligned data\n", |
| 3880 | __func__); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3881 | return -EINVAL; |
| 3882 | } |
| 3883 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3884 | column = to & (mtd->writesize - 1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3885 | |
Thomas Gleixner | 6a93096 | 2006-06-28 00:11:45 +0200 | [diff] [blame] | 3886 | chipnr = (int)(to >> chip->chip_shift); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3887 | chip->select_chip(chip, chipnr); |
Thomas Gleixner | 6a93096 | 2006-06-28 00:11:45 +0200 | [diff] [blame] | 3888 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3889 | /* Check, if it is write protected */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3890 | if (nand_check_wp(chip)) { |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3891 | ret = -EIO; |
| 3892 | goto err_out; |
| 3893 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3894 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3895 | realpage = (int)(to >> chip->page_shift); |
| 3896 | page = realpage & chip->pagemask; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3897 | |
| 3898 | /* Invalidate the page cache, when we write to the cached page */ |
Brian Norris | 537ab1b | 2014-07-21 19:08:03 -0700 | [diff] [blame] | 3899 | if (to <= ((loff_t)chip->pagebuf << chip->page_shift) && |
| 3900 | ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len)) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3901 | chip->pagebuf = -1; |
| 3902 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3903 | /* Don't allow multipage oob writes with offset */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3904 | if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) { |
| 3905 | ret = -EINVAL; |
| 3906 | goto err_out; |
| 3907 | } |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3908 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3909 | while (1) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3910 | int bytes = mtd->writesize; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3911 | uint8_t *wbuf = buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3912 | int use_bufpoi; |
Hector Palacios | 144f4c9 | 2016-07-18 10:39:18 +0200 | [diff] [blame] | 3913 | int part_pagewr = (column || writelen < mtd->writesize); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3914 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3915 | if (part_pagewr) |
| 3916 | use_bufpoi = 1; |
| 3917 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 3918 | use_bufpoi = !virt_addr_valid(buf) || |
| 3919 | !IS_ALIGNED((unsigned long)buf, |
| 3920 | chip->buf_align); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3921 | else |
| 3922 | use_bufpoi = 0; |
| 3923 | |
| 3924 | /* Partial page write?, or need to use bounce buffer */ |
| 3925 | if (use_bufpoi) { |
| 3926 | pr_debug("%s: using write bounce buffer for buf@%p\n", |
| 3927 | __func__, buf); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3928 | if (part_pagewr) |
| 3929 | bytes = min_t(int, bytes - column, writelen); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3930 | chip->pagebuf = -1; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3931 | memset(chip->data_buf, 0xff, mtd->writesize); |
| 3932 | memcpy(&chip->data_buf[column], buf, bytes); |
| 3933 | wbuf = chip->data_buf; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3934 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3935 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3936 | if (unlikely(oob)) { |
| 3937 | size_t len = min(oobwritelen, oobmaxlen); |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3938 | oob = nand_fill_oob(chip, oob, len, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3939 | oobwritelen -= len; |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 3940 | } else { |
| 3941 | /* We still need to erase leftover OOB data */ |
| 3942 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 3943 | } |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 3944 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3945 | ret = nand_write_page(chip, column, bytes, wbuf, |
Boris Brezillon | 0b4773fd | 2017-05-16 00:17:41 +0200 | [diff] [blame] | 3946 | oob_required, page, |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 3947 | (ops->mode == MTD_OPS_RAW)); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3948 | if (ret) |
| 3949 | break; |
| 3950 | |
| 3951 | writelen -= bytes; |
| 3952 | if (!writelen) |
| 3953 | break; |
| 3954 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 3955 | column = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3956 | buf += bytes; |
| 3957 | realpage++; |
| 3958 | |
| 3959 | page = realpage & chip->pagemask; |
| 3960 | /* Check, if we cross a chip boundary */ |
| 3961 | if (!page) { |
| 3962 | chipnr++; |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3963 | chip->select_chip(chip, -1); |
| 3964 | chip->select_chip(chip, chipnr); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3965 | } |
| 3966 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3967 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3968 | ops->retlen = ops->len - writelen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3969 | if (unlikely(oob)) |
| 3970 | ops->oobretlen = ops->ooblen; |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3971 | |
| 3972 | err_out: |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3973 | chip->select_chip(chip, -1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3974 | return ret; |
| 3975 | } |
| 3976 | |
| 3977 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 3978 | * panic_nand_write - [MTD Interface] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3979 | * @mtd: MTD device structure |
| 3980 | * @to: offset to write to |
| 3981 | * @len: number of bytes to write |
| 3982 | * @retlen: pointer to variable to store the number of written bytes |
| 3983 | * @buf: the data to write |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 3984 | * |
| 3985 | * NAND write with ECC. Used when performing writes in interrupt context, this |
| 3986 | * may for example be called by mtdoops when writing an oops while in panic. |
| 3987 | */ |
| 3988 | static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 3989 | size_t *retlen, const uint8_t *buf) |
| 3990 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3991 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brent Taylor | 30863e38 | 2017-10-30 22:32:45 -0500 | [diff] [blame] | 3992 | int chipnr = (int)(to >> chip->chip_shift); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 3993 | struct mtd_oob_ops ops; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 3994 | int ret; |
| 3995 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3996 | /* Grab the device */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 3997 | panic_nand_get_device(chip, FL_WRITING); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 3998 | |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3999 | chip->select_chip(chip, chipnr); |
Brent Taylor | 30863e38 | 2017-10-30 22:32:45 -0500 | [diff] [blame] | 4000 | |
| 4001 | /* Wait for the device to get ready */ |
Boris Brezillon | f1d4694 | 2018-09-06 14:05:29 +0200 | [diff] [blame] | 4002 | panic_nand_wait(chip, 400); |
Brent Taylor | 30863e38 | 2017-10-30 22:32:45 -0500 | [diff] [blame] | 4003 | |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 4004 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 4005 | ops.len = len; |
| 4006 | ops.datbuf = (uint8_t *)buf; |
Huang Shijie | 11041ae6 | 2012-07-03 16:44:14 +0800 | [diff] [blame] | 4007 | ops.mode = MTD_OPS_PLACE_OOB; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4008 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4009 | ret = nand_do_write_ops(chip, to, &ops); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4010 | |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 4011 | *retlen = ops.retlen; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4012 | return ret; |
| 4013 | } |
| 4014 | |
| 4015 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4016 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4017 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4018 | * @to: offset to write to |
| 4019 | * @ops: oob operation description structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4020 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4021 | * NAND write out-of-band. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4022 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4023 | static int nand_do_write_oob(struct nand_chip *chip, loff_t to, |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4024 | struct mtd_oob_ops *ops) |
| 4025 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4026 | struct mtd_info *mtd = nand_to_mtd(chip); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 4027 | int chipnr, page, status, len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4028 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4029 | pr_debug("%s: to = 0x%08x, len = %i\n", |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 4030 | __func__, (unsigned int)to, (int)ops->ooblen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4031 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 4032 | len = mtd_oobavail(mtd, ops); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 4033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4034 | /* Do not allow write past end of page */ |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 4035 | if ((ops->ooboffs + ops->ooblen) > len) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4036 | pr_debug("%s: attempt to write past end of page\n", |
| 4037 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4038 | return -EINVAL; |
| 4039 | } |
| 4040 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 4041 | chipnr = (int)(to >> chip->chip_shift); |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 4042 | |
| 4043 | /* |
| 4044 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one |
| 4045 | * of my DiskOnChip 2000 test units) will clear the whole data page too |
| 4046 | * if we don't do this. I have no clue why, but I seem to have 'fixed' |
| 4047 | * it in the doc2000 driver in August 1999. dwmw2. |
| 4048 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4049 | nand_reset(chip, chipnr); |
| 4050 | |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4051 | chip->select_chip(chip, chipnr); |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4052 | |
| 4053 | /* Shift to get page */ |
| 4054 | page = (int)(to >> chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4055 | |
| 4056 | /* Check, if it is write protected */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4057 | if (nand_check_wp(chip)) { |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4058 | chip->select_chip(chip, -1); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4059 | return -EROFS; |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 4060 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4062 | /* Invalidate the page cache, if we write to the cached page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4063 | if (page == chip->pagebuf) |
| 4064 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4065 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4066 | nand_fill_oob(chip, ops->oobbuf, ops->ooblen, ops); |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 4067 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 4068 | if (ops->mode == MTD_OPS_RAW) |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4069 | status = chip->ecc.write_oob_raw(chip, page & chip->pagemask); |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 4070 | else |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4071 | status = chip->ecc.write_oob(chip, page & chip->pagemask); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4072 | |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4073 | chip->select_chip(chip, -1); |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 4074 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 4075 | if (status) |
| 4076 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4077 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 4078 | ops->oobretlen = ops->ooblen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4079 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 4080 | return 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4081 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4082 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4083 | /** |
| 4084 | * 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] | 4085 | * @mtd: MTD device structure |
| 4086 | * @to: offset to write to |
| 4087 | * @ops: oob operation description structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4088 | */ |
| 4089 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, |
| 4090 | struct mtd_oob_ops *ops) |
| 4091 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4092 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4093 | int ret = -ENOTSUPP; |
| 4094 | |
| 4095 | ops->retlen = 0; |
| 4096 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4097 | nand_get_device(chip, FL_WRITING); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4098 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 4099 | switch (ops->mode) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 4100 | case MTD_OPS_PLACE_OOB: |
| 4101 | case MTD_OPS_AUTO_OOB: |
| 4102 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4103 | break; |
| 4104 | |
| 4105 | default: |
| 4106 | goto out; |
| 4107 | } |
| 4108 | |
| 4109 | if (!ops->datbuf) |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4110 | ret = nand_do_write_oob(chip, to, ops); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4111 | else |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4112 | ret = nand_do_write_ops(chip, to, ops); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4113 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4114 | out: |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4115 | nand_release_device(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4116 | return ret; |
| 4117 | } |
| 4118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4119 | /** |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4120 | * single_erase - [GENERIC] NAND standard block erase command function |
Boris Brezillon | a2098a9 | 2018-09-06 14:05:30 +0200 | [diff] [blame] | 4121 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4122 | * @page: the page address of the block which will be erased |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4123 | * |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4124 | * Standard erase command for NAND chips. Returns NAND status. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4125 | */ |
Boris Brezillon | a2098a9 | 2018-09-06 14:05:30 +0200 | [diff] [blame] | 4126 | static int single_erase(struct nand_chip *chip, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4127 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4128 | unsigned int eraseblock; |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4130 | /* Send commands to erase a block */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4131 | eraseblock = page >> (chip->phys_erase_shift - chip->page_shift); |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4132 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4133 | return nand_erase_op(chip, eraseblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4134 | } |
| 4135 | |
| 4136 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4137 | * nand_erase - [MTD Interface] erase block(s) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4138 | * @mtd: MTD device structure |
| 4139 | * @instr: erase instruction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4140 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4141 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4142 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4143 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4144 | { |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 4145 | return nand_erase_nand(mtd_to_nand(mtd), instr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4146 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4148 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 4149 | * nand_erase_nand - [INTERN] erase block(s) |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 4150 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4151 | * @instr: erase instruction |
| 4152 | * @allowbbt: allow erasing the bbt area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4153 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4154 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4155 | */ |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 4156 | int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr, |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4157 | int allowbbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4158 | { |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4159 | int page, status, pages_per_block, ret, chipnr; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4160 | loff_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4161 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4162 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
| 4163 | __func__, (unsigned long long)instr->addr, |
| 4164 | (unsigned long long)instr->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4166 | if (check_offs_len(chip, instr->addr, instr->len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4167 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4169 | /* Grab the lock and see if the device is available */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4170 | nand_get_device(chip, FL_ERASING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4171 | |
| 4172 | /* Shift to get first page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4173 | page = (int)(instr->addr >> chip->page_shift); |
| 4174 | chipnr = (int)(instr->addr >> chip->chip_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4175 | |
| 4176 | /* Calculate pages in each block */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4177 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4178 | |
| 4179 | /* Select the NAND device */ |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4180 | chip->select_chip(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4182 | /* Check, if it is write protected */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4183 | if (nand_check_wp(chip)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4184 | pr_debug("%s: device is write protected!\n", |
| 4185 | __func__); |
Boris Brezillon | e7bfb3f | 2018-02-12 22:03:11 +0100 | [diff] [blame] | 4186 | ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4187 | goto erase_exit; |
| 4188 | } |
| 4189 | |
| 4190 | /* Loop through the pages */ |
| 4191 | len = instr->len; |
| 4192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4193 | while (len) { |
Wolfram Sang | 12183a2 | 2011-12-21 23:01:20 +0100 | [diff] [blame] | 4194 | /* Check if we have a bad block, we do not erase bad blocks! */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4195 | if (nand_block_checkbad(chip, ((loff_t) page) << |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4196 | chip->page_shift, allowbbt)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 4197 | pr_warn("%s: attempt to erase a bad block at page 0x%08x\n", |
| 4198 | __func__, page); |
Boris Brezillon | e7bfb3f | 2018-02-12 22:03:11 +0100 | [diff] [blame] | 4199 | ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4200 | goto erase_exit; |
| 4201 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4202 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4203 | /* |
| 4204 | * Invalidate the page cache, if we erase the block which |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4205 | * contains the current cached page. |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4206 | */ |
| 4207 | if (page <= chip->pagebuf && chip->pagebuf < |
| 4208 | (page + pages_per_block)) |
| 4209 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4210 | |
Boris Brezillon | f9ebd1b | 2018-09-07 00:38:39 +0200 | [diff] [blame] | 4211 | if (chip->legacy.erase) |
| 4212 | status = chip->legacy.erase(chip, |
| 4213 | page & chip->pagemask); |
| 4214 | else |
| 4215 | status = single_erase(chip, page & chip->pagemask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4216 | |
| 4217 | /* See if block erase succeeded */ |
Miquel Raynal | eb94555 | 2017-11-30 18:01:28 +0100 | [diff] [blame] | 4218 | if (status) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4219 | pr_debug("%s: failed erase, page 0x%08x\n", |
| 4220 | __func__, page); |
Boris Brezillon | e7bfb3f | 2018-02-12 22:03:11 +0100 | [diff] [blame] | 4221 | ret = -EIO; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4222 | instr->fail_addr = |
| 4223 | ((loff_t)page << chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4224 | goto erase_exit; |
| 4225 | } |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 4226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4227 | /* Increment page address and decrement length */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 4228 | len -= (1ULL << chip->phys_erase_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4229 | page += pages_per_block; |
| 4230 | |
| 4231 | /* Check, if we cross a chip boundary */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4232 | if (len && !(page & chip->pagemask)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4233 | chipnr++; |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4234 | chip->select_chip(chip, -1); |
| 4235 | chip->select_chip(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4236 | } |
| 4237 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4238 | |
Boris Brezillon | e7bfb3f | 2018-02-12 22:03:11 +0100 | [diff] [blame] | 4239 | ret = 0; |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4240 | erase_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4242 | /* Deselect and wake up anyone waiting on the device */ |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4243 | chip->select_chip(chip, -1); |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4244 | nand_release_device(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4246 | /* Return more or less happy */ |
| 4247 | return ret; |
| 4248 | } |
| 4249 | |
| 4250 | /** |
| 4251 | * nand_sync - [MTD Interface] sync |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4252 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4253 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4254 | * Sync is actually a wait for chip ready function. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4255 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4256 | static void nand_sync(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4257 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4258 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 4259 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4260 | pr_debug("%s: called\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4261 | |
| 4262 | /* Grab the lock and see if the device is available */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4263 | nand_get_device(chip, FL_SYNCING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4264 | /* Release it and go back */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4265 | nand_release_device(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4266 | } |
| 4267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4268 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4269 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4270 | * @mtd: MTD device structure |
| 4271 | * @offs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4272 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4273 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4274 | { |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4275 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 4276 | int chipnr = (int)(offs >> chip->chip_shift); |
| 4277 | int ret; |
| 4278 | |
| 4279 | /* Select the NAND device */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4280 | nand_get_device(chip, FL_READING); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4281 | chip->select_chip(chip, chipnr); |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4282 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4283 | ret = nand_block_checkbad(chip, offs, 0); |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4284 | |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4285 | chip->select_chip(chip, -1); |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4286 | nand_release_device(chip); |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4287 | |
| 4288 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4289 | } |
| 4290 | |
| 4291 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4292 | * 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] | 4293 | * @mtd: MTD device structure |
| 4294 | * @ofs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4295 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4296 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4297 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4298 | int ret; |
| 4299 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 4300 | ret = nand_block_isbad(mtd, ofs); |
| 4301 | if (ret) { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4302 | /* If it was bad already, return success and do nothing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4303 | if (ret > 0) |
| 4304 | return 0; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4305 | return ret; |
| 4306 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4307 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4308 | return nand_block_markbad_lowlevel(mtd_to_nand(mtd), ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4309 | } |
| 4310 | |
| 4311 | /** |
Zach Brown | 5671842 | 2017-01-10 13:30:20 -0600 | [diff] [blame] | 4312 | * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd |
| 4313 | * @mtd: MTD device structure |
| 4314 | * @ofs: offset relative to mtd start |
| 4315 | * @len: length of mtd |
| 4316 | */ |
| 4317 | static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len) |
| 4318 | { |
| 4319 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 4320 | u32 part_start_block; |
| 4321 | u32 part_end_block; |
| 4322 | u32 part_start_die; |
| 4323 | u32 part_end_die; |
| 4324 | |
| 4325 | /* |
| 4326 | * max_bb_per_die and blocks_per_die used to determine |
| 4327 | * the maximum bad block count. |
| 4328 | */ |
| 4329 | if (!chip->max_bb_per_die || !chip->blocks_per_die) |
| 4330 | return -ENOTSUPP; |
| 4331 | |
| 4332 | /* Get the start and end of the partition in erase blocks. */ |
| 4333 | part_start_block = mtd_div_by_eb(ofs, mtd); |
| 4334 | part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1; |
| 4335 | |
| 4336 | /* Get the start and end LUNs of the partition. */ |
| 4337 | part_start_die = part_start_block / chip->blocks_per_die; |
| 4338 | part_end_die = part_end_block / chip->blocks_per_die; |
| 4339 | |
| 4340 | /* |
| 4341 | * Look up the bad blocks per unit and multiply by the number of units |
| 4342 | * that the partition spans. |
| 4343 | */ |
| 4344 | return chip->max_bb_per_die * (part_end_die - part_start_die + 1); |
| 4345 | } |
| 4346 | |
| 4347 | /** |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4348 | * nand_suspend - [MTD Interface] Suspend the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4349 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4350 | */ |
| 4351 | static int nand_suspend(struct mtd_info *mtd) |
| 4352 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4353 | return nand_get_device(mtd_to_nand(mtd), FL_PM_SUSPENDED); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4354 | } |
| 4355 | |
| 4356 | /** |
| 4357 | * nand_resume - [MTD Interface] Resume the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4358 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4359 | */ |
| 4360 | static void nand_resume(struct mtd_info *mtd) |
| 4361 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4362 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4363 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4364 | if (chip->state == FL_PM_SUSPENDED) |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4365 | nand_release_device(chip); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4366 | else |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 4367 | pr_err("%s called for a chip which is not in suspended state\n", |
| 4368 | __func__); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4369 | } |
| 4370 | |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 4371 | /** |
| 4372 | * nand_shutdown - [MTD Interface] Finish the current NAND operation and |
| 4373 | * prevent further operations |
| 4374 | * @mtd: MTD device structure |
| 4375 | */ |
| 4376 | static void nand_shutdown(struct mtd_info *mtd) |
| 4377 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 4378 | nand_get_device(mtd_to_nand(mtd), FL_PM_SUSPENDED); |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 4379 | } |
| 4380 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4381 | /* Set default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4382 | static void nand_set_defaults(struct nand_chip *chip) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4383 | { |
Boris Brezillon | 3d4af7c | 2018-09-07 00:38:49 +0200 | [diff] [blame] | 4384 | nand_legacy_set_defaults(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4385 | |
| 4386 | if (!chip->controller) { |
Miquel Raynal | 7da4513 | 2018-07-17 09:08:02 +0200 | [diff] [blame] | 4387 | chip->controller = &chip->dummy_controller; |
| 4388 | nand_controller_init(chip->controller); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4389 | } |
| 4390 | |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 4391 | if (!chip->buf_align) |
| 4392 | chip->buf_align = 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4393 | } |
| 4394 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4395 | /* Sanitize ONFI strings so we can safely print them */ |
Boris Brezillon | 1c325cc | 2018-09-07 00:38:50 +0200 | [diff] [blame] | 4396 | void sanitize_string(uint8_t *s, size_t len) |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4397 | { |
| 4398 | ssize_t i; |
| 4399 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4400 | /* Null terminate */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4401 | s[len - 1] = 0; |
| 4402 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4403 | /* Remove non printable chars */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4404 | for (i = 0; i < len - 1; i++) { |
| 4405 | if (s[i] < ' ' || s[i] > 127) |
| 4406 | s[i] = '?'; |
| 4407 | } |
| 4408 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4409 | /* Remove trailing spaces */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4410 | strim(s); |
| 4411 | } |
| 4412 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4413 | /* |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 4414 | * nand_id_has_period - Check if an ID string has a given wraparound period |
| 4415 | * @id_data: the ID string |
| 4416 | * @arrlen: the length of the @id_data array |
| 4417 | * @period: the period of repitition |
| 4418 | * |
| 4419 | * Check if an ID string is repeated within a given sequence of bytes at |
| 4420 | * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a |
Brian Norris | d4d4f1b | 2012-11-14 21:54:20 -0800 | [diff] [blame] | 4421 | * 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] | 4422 | * if the repetition has a period of @period; otherwise, returns zero. |
| 4423 | */ |
| 4424 | static int nand_id_has_period(u8 *id_data, int arrlen, int period) |
| 4425 | { |
| 4426 | int i, j; |
| 4427 | for (i = 0; i < period; i++) |
| 4428 | for (j = i + period; j < arrlen; j += period) |
| 4429 | if (id_data[i] != id_data[j]) |
| 4430 | return 0; |
| 4431 | return 1; |
| 4432 | } |
| 4433 | |
| 4434 | /* |
| 4435 | * nand_id_len - Get the length of an ID string returned by CMD_READID |
| 4436 | * @id_data: the ID string |
| 4437 | * @arrlen: the length of the @id_data array |
| 4438 | |
| 4439 | * Returns the length of the ID string, according to known wraparound/trailing |
| 4440 | * zero patterns. If no pattern exists, returns the length of the array. |
| 4441 | */ |
| 4442 | static int nand_id_len(u8 *id_data, int arrlen) |
| 4443 | { |
| 4444 | int last_nonzero, period; |
| 4445 | |
| 4446 | /* Find last non-zero byte */ |
| 4447 | for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--) |
| 4448 | if (id_data[last_nonzero]) |
| 4449 | break; |
| 4450 | |
| 4451 | /* All zeros */ |
| 4452 | if (last_nonzero < 0) |
| 4453 | return 0; |
| 4454 | |
| 4455 | /* Calculate wraparound period */ |
| 4456 | for (period = 1; period < arrlen; period++) |
| 4457 | if (nand_id_has_period(id_data, arrlen, period)) |
| 4458 | break; |
| 4459 | |
| 4460 | /* There's a repeated pattern */ |
| 4461 | if (period < arrlen) |
| 4462 | return period; |
| 4463 | |
| 4464 | /* There are trailing zeros */ |
| 4465 | if (last_nonzero < arrlen - 1) |
| 4466 | return last_nonzero + 1; |
| 4467 | |
| 4468 | /* No pattern detected */ |
| 4469 | return arrlen; |
| 4470 | } |
| 4471 | |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 4472 | /* Extract the bits of per cell from the 3rd byte of the extended ID */ |
| 4473 | static int nand_get_bits_per_cell(u8 cellinfo) |
| 4474 | { |
| 4475 | int bits; |
| 4476 | |
| 4477 | bits = cellinfo & NAND_CI_CELLTYPE_MSK; |
| 4478 | bits >>= NAND_CI_CELLTYPE_SHIFT; |
| 4479 | return bits + 1; |
| 4480 | } |
| 4481 | |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 4482 | /* |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4483 | * Many new NAND share similar device ID codes, which represent the size of the |
| 4484 | * chip. The rest of the parameters must be decoded according to generic or |
| 4485 | * manufacturer-specific "extended ID" decoding patterns. |
| 4486 | */ |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4487 | void nand_decode_ext_id(struct nand_chip *chip) |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4488 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4489 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 9b2d61f | 2016-06-08 10:34:57 +0200 | [diff] [blame] | 4490 | int extid; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4491 | u8 *id_data = chip->id.data; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4492 | /* The 3rd id byte holds MLC / multichip data */ |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 4493 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4494 | /* The 4th id byte is the important one */ |
| 4495 | extid = id_data[3]; |
| 4496 | |
Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 4497 | /* Calc pagesize */ |
| 4498 | mtd->writesize = 1024 << (extid & 0x03); |
| 4499 | extid >>= 2; |
| 4500 | /* Calc oobsize */ |
| 4501 | mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9); |
| 4502 | extid >>= 2; |
| 4503 | /* Calc blocksize. Blocksize is multiples of 64KiB */ |
| 4504 | mtd->erasesize = (64 * 1024) << (extid & 0x03); |
| 4505 | extid >>= 2; |
| 4506 | /* Get buswidth information */ |
| 4507 | if (extid & 0x1) |
| 4508 | chip->options |= NAND_BUSWIDTH_16; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4509 | } |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4510 | EXPORT_SYMBOL_GPL(nand_decode_ext_id); |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4511 | |
| 4512 | /* |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 4513 | * Old devices have chip data hardcoded in the device ID table. nand_decode_id |
| 4514 | * decodes a matching ID table entry and assigns the MTD size parameters for |
| 4515 | * the chip. |
| 4516 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4517 | 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] | 4518 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4519 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 4520 | |
| 4521 | mtd->erasesize = type->erasesize; |
| 4522 | mtd->writesize = type->pagesize; |
| 4523 | mtd->oobsize = mtd->writesize / 32; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 4524 | |
Huang Shijie | 1c195e9 | 2013-09-25 14:58:12 +0800 | [diff] [blame] | 4525 | /* All legacy ID NAND are small-page, SLC */ |
| 4526 | chip->bits_per_cell = 1; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 4527 | } |
| 4528 | |
| 4529 | /* |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4530 | * Set the bad block marker/indicator (BBM/BBI) patterns according to some |
| 4531 | * heuristic patterns using various detected parameters (e.g., manufacturer, |
| 4532 | * page size, cell-type information). |
| 4533 | */ |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4534 | static void nand_decode_bbm_options(struct nand_chip *chip) |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4535 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4536 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4537 | |
| 4538 | /* Set the bad block position */ |
| 4539 | if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16)) |
| 4540 | chip->badblockpos = NAND_LARGE_BADBLOCK_POS; |
| 4541 | else |
| 4542 | chip->badblockpos = NAND_SMALL_BADBLOCK_POS; |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4543 | } |
| 4544 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4545 | static inline bool is_full_id_nand(struct nand_flash_dev *type) |
| 4546 | { |
| 4547 | return type->id_len; |
| 4548 | } |
| 4549 | |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4550 | static bool find_full_id_nand(struct nand_chip *chip, |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4551 | struct nand_flash_dev *type) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4552 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4553 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4554 | u8 *id_data = chip->id.data; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4555 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4556 | if (!strncmp(type->id, id_data, type->id_len)) { |
| 4557 | mtd->writesize = type->pagesize; |
| 4558 | mtd->erasesize = type->erasesize; |
| 4559 | mtd->oobsize = type->oobsize; |
| 4560 | |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 4561 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4562 | chip->chipsize = (uint64_t)type->chipsize << 20; |
| 4563 | chip->options |= type->options; |
Huang Shijie | 5721934 | 2013-05-17 11:17:32 +0800 | [diff] [blame] | 4564 | chip->ecc_strength_ds = NAND_ECC_STRENGTH(type); |
| 4565 | chip->ecc_step_ds = NAND_ECC_STEP(type); |
Boris BREZILLON | 57a94e2 | 2014-09-22 20:11:50 +0200 | [diff] [blame] | 4566 | chip->onfi_timing_mode_default = |
| 4567 | type->onfi_timing_mode_default; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4568 | |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 4569 | chip->parameters.model = kstrdup(type->name, GFP_KERNEL); |
| 4570 | if (!chip->parameters.model) |
| 4571 | return false; |
Cai Zhiyong | 092b6a1 | 2013-12-25 21:19:21 +0800 | [diff] [blame] | 4572 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4573 | return true; |
| 4574 | } |
| 4575 | return false; |
| 4576 | } |
| 4577 | |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4578 | /* |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4579 | * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC |
| 4580 | * compliant and does not have a full-id or legacy-id entry in the nand_ids |
| 4581 | * table. |
| 4582 | */ |
| 4583 | static void nand_manufacturer_detect(struct nand_chip *chip) |
| 4584 | { |
| 4585 | /* |
| 4586 | * Try manufacturer detection if available and use |
| 4587 | * nand_decode_ext_id() otherwise. |
| 4588 | */ |
| 4589 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 4590 | chip->manufacturer.desc->ops->detect) { |
| 4591 | /* The 3rd id byte holds MLC / multichip data */ |
| 4592 | 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] | 4593 | chip->manufacturer.desc->ops->detect(chip); |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 4594 | } else { |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4595 | nand_decode_ext_id(chip); |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 4596 | } |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4597 | } |
| 4598 | |
| 4599 | /* |
| 4600 | * Manufacturer initialization. This function is called for all NANDs including |
| 4601 | * ONFI and JEDEC compliant ones. |
| 4602 | * Manufacturer drivers should put all their specific initialization code in |
| 4603 | * their ->init() hook. |
| 4604 | */ |
| 4605 | static int nand_manufacturer_init(struct nand_chip *chip) |
| 4606 | { |
| 4607 | if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops || |
| 4608 | !chip->manufacturer.desc->ops->init) |
| 4609 | return 0; |
| 4610 | |
| 4611 | return chip->manufacturer.desc->ops->init(chip); |
| 4612 | } |
| 4613 | |
| 4614 | /* |
| 4615 | * Manufacturer cleanup. This function is called for all NANDs including |
| 4616 | * ONFI and JEDEC compliant ones. |
| 4617 | * Manufacturer drivers should put all their specific cleanup code in their |
| 4618 | * ->cleanup() hook. |
| 4619 | */ |
| 4620 | static void nand_manufacturer_cleanup(struct nand_chip *chip) |
| 4621 | { |
| 4622 | /* Release manufacturer private data */ |
| 4623 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
| 4624 | chip->manufacturer.desc->ops->cleanup) |
| 4625 | chip->manufacturer.desc->ops->cleanup(chip); |
| 4626 | } |
| 4627 | |
Boris Brezillon | 348d56a | 2018-09-07 00:38:48 +0200 | [diff] [blame] | 4628 | static const char * |
| 4629 | nand_manufacturer_name(const struct nand_manufacturer *manufacturer) |
| 4630 | { |
| 4631 | return manufacturer ? manufacturer->name : "Unknown"; |
| 4632 | } |
| 4633 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4634 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4635 | * 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] | 4636 | */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame] | 4637 | 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] | 4638 | { |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4639 | const struct nand_manufacturer *manufacturer; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4640 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4641 | int busw, ret; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4642 | u8 *id_data = chip->id.data; |
| 4643 | u8 maf_id, dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4644 | |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4645 | /* |
| 4646 | * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4647 | * after power-up. |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4648 | */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4649 | ret = nand_reset(chip, 0); |
| 4650 | if (ret) |
| 4651 | return ret; |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4652 | |
| 4653 | /* Select the device */ |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4654 | chip->select_chip(chip, 0); |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4656 | /* Send the command for reading device ID */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4657 | ret = nand_readid_op(chip, 0, id_data, 2); |
| 4658 | if (ret) |
| 4659 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4660 | |
| 4661 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4662 | maf_id = id_data[0]; |
| 4663 | dev_id = id_data[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4664 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4665 | /* |
| 4666 | * 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] | 4667 | * interface concerns can cause random data which looks like a |
| 4668 | * possibly credible NAND flash to appear. If the two results do |
| 4669 | * not match, ignore the device completely. |
| 4670 | */ |
| 4671 | |
Brian Norris | 4aef9b7 | 2012-09-24 20:40:48 -0700 | [diff] [blame] | 4672 | /* Read entire ID string */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4673 | ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data)); |
| 4674 | if (ret) |
| 4675 | return ret; |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 4676 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4677 | if (id_data[0] != maf_id || id_data[1] != dev_id) { |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4678 | 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] | 4679 | maf_id, dev_id, id_data[0], id_data[1]); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4680 | return -ENODEV; |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 4681 | } |
| 4682 | |
Jean-Louis Thekekara | 5158bd5 | 2017-06-29 19:08:30 +0200 | [diff] [blame] | 4683 | 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] | 4684 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4685 | /* Try to identify manufacturer */ |
| 4686 | manufacturer = nand_get_manufacturer(maf_id); |
| 4687 | chip->manufacturer.desc = manufacturer; |
| 4688 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4689 | if (!type) |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4690 | type = nand_flash_ids; |
| 4691 | |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4692 | /* |
| 4693 | * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic |
| 4694 | * override it. |
| 4695 | * This is required to make sure initial NAND bus width set by the |
| 4696 | * NAND controller driver is coherent with the real NAND bus width |
| 4697 | * (extracted by auto-detection code). |
| 4698 | */ |
| 4699 | busw = chip->options & NAND_BUSWIDTH_16; |
| 4700 | |
| 4701 | /* |
| 4702 | * The flag is only set (never cleared), reset it to its default value |
| 4703 | * before starting auto-detection. |
| 4704 | */ |
| 4705 | chip->options &= ~NAND_BUSWIDTH_16; |
| 4706 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4707 | for (; type->name != NULL; type++) { |
| 4708 | if (is_full_id_nand(type)) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4709 | if (find_full_id_nand(chip, type)) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4710 | goto ident_done; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4711 | } else if (dev_id == type->dev_id) { |
Brian Norris | db5b09f | 2015-05-22 10:43:12 -0700 | [diff] [blame] | 4712 | break; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4713 | } |
| 4714 | } |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4715 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4716 | if (!type->name || !type->pagesize) { |
Masahiro Yamada | 35fc519 | 2014-04-09 16:26:26 +0900 | [diff] [blame] | 4717 | /* Check if the chip is ONFI compliant */ |
Boris Brezillon | 1c325cc | 2018-09-07 00:38:50 +0200 | [diff] [blame] | 4718 | ret = nand_onfi_detect(chip); |
Miquel Raynal | bd0b643 | 2018-03-19 14:47:31 +0100 | [diff] [blame] | 4719 | if (ret < 0) |
| 4720 | return ret; |
| 4721 | else if (ret) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4722 | goto ident_done; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4723 | |
| 4724 | /* Check if the chip is JEDEC compliant */ |
Boris Brezillon | 8ae3fbf | 2018-09-07 00:38:51 +0200 | [diff] [blame] | 4725 | ret = nand_jedec_detect(chip); |
Miquel Raynal | 480139d | 2018-03-19 14:47:30 +0100 | [diff] [blame] | 4726 | if (ret < 0) |
| 4727 | return ret; |
| 4728 | else if (ret) |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4729 | goto ident_done; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4730 | } |
| 4731 | |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4732 | if (!type->name) |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4733 | return -ENODEV; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4734 | |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 4735 | chip->parameters.model = kstrdup(type->name, GFP_KERNEL); |
| 4736 | if (!chip->parameters.model) |
| 4737 | return -ENOMEM; |
Thomas Gleixner | ba0251fe | 2006-05-27 01:02:13 +0200 | [diff] [blame] | 4738 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4739 | chip->chipsize = (uint64_t)type->chipsize << 20; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4740 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4741 | if (!type->pagesize) |
| 4742 | nand_manufacturer_detect(chip); |
| 4743 | else |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4744 | nand_decode_id(chip, type); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 4745 | |
Brian Norris | bf7a01b | 2012-07-13 09:28:24 -0700 | [diff] [blame] | 4746 | /* Get chip options */ |
| 4747 | chip->options |= type->options; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4748 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4749 | ident_done: |
Miquel Raynal | f4531b2 | 2018-03-19 14:47:26 +0100 | [diff] [blame] | 4750 | if (!mtd->name) |
| 4751 | mtd->name = chip->parameters.model; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4752 | |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 4753 | if (chip->options & NAND_BUSWIDTH_AUTO) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4754 | WARN_ON(busw & NAND_BUSWIDTH_16); |
| 4755 | nand_set_defaults(chip); |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 4756 | } else if (busw != (chip->options & NAND_BUSWIDTH_16)) { |
| 4757 | /* |
| 4758 | * Check, if buswidth is correct. Hardware drivers should set |
| 4759 | * chip correct! |
| 4760 | */ |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4761 | 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] | 4762 | maf_id, dev_id); |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4763 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4764 | mtd->name); |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4765 | pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8, |
| 4766 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8); |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 4767 | ret = -EINVAL; |
| 4768 | |
| 4769 | goto free_detect_allocation; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4770 | } |
| 4771 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4772 | nand_decode_bbm_options(chip); |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4773 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4774 | /* Calculate the address shift from the page size */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4775 | chip->page_shift = ffs(mtd->writesize) - 1; |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4776 | /* Convert chipsize to number of pages per chip -1 */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4777 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4778 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4779 | chip->bbt_erase_shift = chip->phys_erase_shift = |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4780 | ffs(mtd->erasesize) - 1; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4781 | if (chip->chipsize & 0xffffffff) |
| 4782 | chip->chip_shift = ffs((unsigned)chip->chipsize) - 1; |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4783 | else { |
| 4784 | chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)); |
| 4785 | chip->chip_shift += 32 - 1; |
| 4786 | } |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4787 | |
Masahiro Yamada | 14157f8 | 2017-09-13 11:05:50 +0900 | [diff] [blame] | 4788 | if (chip->chip_shift - chip->page_shift > 16) |
| 4789 | chip->options |= NAND_ROW_ADDR_3; |
| 4790 | |
Artem Bityutskiy | 26d9be1 | 2011-04-28 20:26:59 +0300 | [diff] [blame] | 4791 | chip->badblockbits = 8; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4792 | |
Boris Brezillon | 3d4af7c | 2018-09-07 00:38:49 +0200 | [diff] [blame] | 4793 | nand_legacy_adjust_cmdfunc(chip); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4794 | |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4795 | 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] | 4796 | maf_id, dev_id); |
Miquel Raynal | f4531b2 | 2018-03-19 14:47:26 +0100 | [diff] [blame] | 4797 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4798 | chip->parameters.model); |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 4799 | 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] | 4800 | (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC", |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 4801 | mtd->erasesize >> 10, mtd->writesize, mtd->oobsize); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4802 | return 0; |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 4803 | |
| 4804 | free_detect_allocation: |
| 4805 | kfree(chip->parameters.model); |
| 4806 | |
| 4807 | return ret; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4808 | } |
| 4809 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4810 | static const char * const nand_ecc_modes[] = { |
| 4811 | [NAND_ECC_NONE] = "none", |
| 4812 | [NAND_ECC_SOFT] = "soft", |
| 4813 | [NAND_ECC_HW] = "hw", |
| 4814 | [NAND_ECC_HW_SYNDROME] = "hw_syndrome", |
| 4815 | [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first", |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 4816 | [NAND_ECC_ON_DIE] = "on-die", |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4817 | }; |
| 4818 | |
| 4819 | static int of_get_nand_ecc_mode(struct device_node *np) |
| 4820 | { |
| 4821 | const char *pm; |
| 4822 | int err, i; |
| 4823 | |
| 4824 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 4825 | if (err < 0) |
| 4826 | return err; |
| 4827 | |
| 4828 | for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++) |
| 4829 | if (!strcasecmp(pm, nand_ecc_modes[i])) |
| 4830 | return i; |
| 4831 | |
Rafał Miłecki | ae211bc | 2016-04-17 22:53:06 +0200 | [diff] [blame] | 4832 | /* |
| 4833 | * For backward compatibility we support few obsoleted values that don't |
| 4834 | * have their mappings into nand_ecc_modes_t anymore (they were merged |
| 4835 | * with other enums). |
| 4836 | */ |
| 4837 | if (!strcasecmp(pm, "soft_bch")) |
| 4838 | return NAND_ECC_SOFT; |
| 4839 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4840 | return -ENODEV; |
| 4841 | } |
| 4842 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4843 | static const char * const nand_ecc_algos[] = { |
| 4844 | [NAND_ECC_HAMMING] = "hamming", |
| 4845 | [NAND_ECC_BCH] = "bch", |
Stefan Agner | f308d73 | 2018-06-24 23:27:22 +0200 | [diff] [blame] | 4846 | [NAND_ECC_RS] = "rs", |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4847 | }; |
| 4848 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4849 | static int of_get_nand_ecc_algo(struct device_node *np) |
| 4850 | { |
| 4851 | const char *pm; |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4852 | int err, i; |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4853 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4854 | err = of_property_read_string(np, "nand-ecc-algo", &pm); |
| 4855 | if (!err) { |
| 4856 | for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++) |
| 4857 | if (!strcasecmp(pm, nand_ecc_algos[i])) |
| 4858 | return i; |
| 4859 | return -ENODEV; |
| 4860 | } |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4861 | |
| 4862 | /* |
| 4863 | * For backward compatibility we also read "nand-ecc-mode" checking |
| 4864 | * for some obsoleted values that were specifying ECC algorithm. |
| 4865 | */ |
| 4866 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 4867 | if (err < 0) |
| 4868 | return err; |
| 4869 | |
| 4870 | if (!strcasecmp(pm, "soft")) |
| 4871 | return NAND_ECC_HAMMING; |
| 4872 | else if (!strcasecmp(pm, "soft_bch")) |
| 4873 | return NAND_ECC_BCH; |
| 4874 | |
| 4875 | return -ENODEV; |
| 4876 | } |
| 4877 | |
| 4878 | static int of_get_nand_ecc_step_size(struct device_node *np) |
| 4879 | { |
| 4880 | int ret; |
| 4881 | u32 val; |
| 4882 | |
| 4883 | ret = of_property_read_u32(np, "nand-ecc-step-size", &val); |
| 4884 | return ret ? ret : val; |
| 4885 | } |
| 4886 | |
| 4887 | static int of_get_nand_ecc_strength(struct device_node *np) |
| 4888 | { |
| 4889 | int ret; |
| 4890 | u32 val; |
| 4891 | |
| 4892 | ret = of_property_read_u32(np, "nand-ecc-strength", &val); |
| 4893 | return ret ? ret : val; |
| 4894 | } |
| 4895 | |
| 4896 | static int of_get_nand_bus_width(struct device_node *np) |
| 4897 | { |
| 4898 | u32 val; |
| 4899 | |
| 4900 | if (of_property_read_u32(np, "nand-bus-width", &val)) |
| 4901 | return 8; |
| 4902 | |
| 4903 | switch (val) { |
| 4904 | case 8: |
| 4905 | case 16: |
| 4906 | return val; |
| 4907 | default: |
| 4908 | return -EIO; |
| 4909 | } |
| 4910 | } |
| 4911 | |
| 4912 | static bool of_get_nand_on_flash_bbt(struct device_node *np) |
| 4913 | { |
| 4914 | return of_property_read_bool(np, "nand-on-flash-bbt"); |
| 4915 | } |
| 4916 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4917 | static int nand_dt_init(struct nand_chip *chip) |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4918 | { |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4919 | struct device_node *dn = nand_get_flash_node(chip); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4920 | int ecc_mode, ecc_algo, ecc_strength, ecc_step; |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4921 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4922 | if (!dn) |
| 4923 | return 0; |
| 4924 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4925 | if (of_get_nand_bus_width(dn) == 16) |
| 4926 | chip->options |= NAND_BUSWIDTH_16; |
| 4927 | |
Stefan Agner | f922bd7 | 2018-06-24 23:27:23 +0200 | [diff] [blame] | 4928 | if (of_property_read_bool(dn, "nand-is-boot-medium")) |
| 4929 | chip->options |= NAND_IS_BOOT_MEDIUM; |
| 4930 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4931 | if (of_get_nand_on_flash_bbt(dn)) |
| 4932 | chip->bbt_options |= NAND_BBT_USE_FLASH; |
| 4933 | |
| 4934 | ecc_mode = of_get_nand_ecc_mode(dn); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4935 | ecc_algo = of_get_nand_ecc_algo(dn); |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4936 | ecc_strength = of_get_nand_ecc_strength(dn); |
| 4937 | ecc_step = of_get_nand_ecc_step_size(dn); |
| 4938 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4939 | if (ecc_mode >= 0) |
| 4940 | chip->ecc.mode = ecc_mode; |
| 4941 | |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4942 | if (ecc_algo >= 0) |
| 4943 | chip->ecc.algo = ecc_algo; |
| 4944 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4945 | if (ecc_strength >= 0) |
| 4946 | chip->ecc.strength = ecc_strength; |
| 4947 | |
| 4948 | if (ecc_step > 0) |
| 4949 | chip->ecc.size = ecc_step; |
| 4950 | |
Boris Brezillon | ba78ee0 | 2016-06-08 17:04:22 +0200 | [diff] [blame] | 4951 | if (of_property_read_bool(dn, "nand-ecc-maximize")) |
| 4952 | chip->ecc.options |= NAND_ECC_MAXIMIZE; |
| 4953 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4954 | return 0; |
| 4955 | } |
| 4956 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4957 | /** |
Miquel Raynal | 98732da | 2018-07-25 15:31:50 +0200 | [diff] [blame] | 4958 | * nand_scan_ident - Scan for the NAND device |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 4959 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4960 | * @maxchips: number of chips to scan for |
| 4961 | * @table: alternative NAND ID table |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4962 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4963 | * This is the first phase of the normal nand_scan() function. It reads the |
| 4964 | * flash ID and sets up MTD fields accordingly. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4965 | * |
Miquel Raynal | 98732da | 2018-07-25 15:31:50 +0200 | [diff] [blame] | 4966 | * This helper used to be called directly from controller drivers that needed |
| 4967 | * to tweak some ECC-related parameters before nand_scan_tail(). This separation |
| 4968 | * prevented dynamic allocations during this phase which was unconvenient and |
| 4969 | * 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] | 4970 | */ |
Boris Brezillon | 871a407 | 2018-08-04 22:59:22 +0200 | [diff] [blame] | 4971 | static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips, |
Miquel Raynal | 98732da | 2018-07-25 15:31:50 +0200 | [diff] [blame] | 4972 | struct nand_flash_dev *table) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4973 | { |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 4974 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 871a407 | 2018-08-04 22:59:22 +0200 | [diff] [blame] | 4975 | int nand_maf_id, nand_dev_id; |
| 4976 | unsigned int i; |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4977 | int ret; |
| 4978 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 4979 | /* Enforce the right timings for reset/detection */ |
| 4980 | onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0); |
| 4981 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4982 | ret = nand_dt_init(chip); |
| 4983 | if (ret) |
| 4984 | return ret; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4985 | |
Brian Norris | f7a8e38 | 2016-01-05 10:39:45 -0800 | [diff] [blame] | 4986 | if (!mtd->name && mtd->dev.parent) |
| 4987 | mtd->name = dev_name(mtd->dev.parent); |
| 4988 | |
Boris Brezillon | 3d4af7c | 2018-09-07 00:38:49 +0200 | [diff] [blame] | 4989 | if (chip->exec_op && !chip->select_chip) { |
| 4990 | pr_err("->select_chip() is mandatory when implementing ->exec_op()\n"); |
| 4991 | return -EINVAL; |
Andrey Smirnov | 76fe334 | 2016-07-21 14:59:20 -0700 | [diff] [blame] | 4992 | } |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 4993 | |
Boris Brezillon | 3d4af7c | 2018-09-07 00:38:49 +0200 | [diff] [blame] | 4994 | ret = nand_legacy_check_hooks(chip); |
| 4995 | if (ret) |
| 4996 | return ret; |
| 4997 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4998 | /* Set the default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4999 | nand_set_defaults(chip); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5000 | |
| 5001 | /* Read the flash type */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame] | 5002 | ret = nand_detect(chip, table); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 5003 | if (ret) { |
Ben Dooks | b1c6e6d | 2009-11-02 18:12:33 +0000 | [diff] [blame] | 5004 | if (!(chip->options & NAND_SCAN_SILENT_NODEV)) |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 5005 | pr_warn("No NAND device found\n"); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 5006 | chip->select_chip(chip, -1); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 5007 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5008 | } |
| 5009 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 5010 | nand_maf_id = chip->id.data[0]; |
| 5011 | nand_dev_id = chip->id.data[1]; |
| 5012 | |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 5013 | chip->select_chip(chip, -1); |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 5014 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5015 | /* Check for a chip array */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5016 | for (i = 1; i < maxchips; i++) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5017 | u8 id[2]; |
| 5018 | |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 5019 | /* See comment in nand_get_flash_type for reset */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 5020 | nand_reset(chip, i); |
| 5021 | |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 5022 | chip->select_chip(chip, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5023 | /* Send the command for reading device ID */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5024 | nand_readid_op(chip, 0, id, sizeof(id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5026 | if (nand_maf_id != id[0] || nand_dev_id != id[1]) { |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 5027 | chip->select_chip(chip, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5028 | break; |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 5029 | } |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 5030 | chip->select_chip(chip, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5031 | } |
| 5032 | if (i > 1) |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 5033 | pr_info("%d chips detected\n", i); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5034 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5035 | /* Store the number of chips and calc total size for mtd */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5036 | chip->numchips = i; |
| 5037 | mtd->size = i * chip->chipsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5038 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5039 | return 0; |
| 5040 | } |
| 5041 | |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5042 | static void nand_scan_ident_cleanup(struct nand_chip *chip) |
| 5043 | { |
| 5044 | kfree(chip->parameters.model); |
Miquel Raynal | 3d3fe3c | 2018-07-25 15:31:52 +0200 | [diff] [blame] | 5045 | kfree(chip->parameters.onfi); |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5046 | } |
| 5047 | |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 5048 | static int nand_set_ecc_soft_ops(struct nand_chip *chip) |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5049 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 5050 | struct mtd_info *mtd = nand_to_mtd(chip); |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5051 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 5052 | |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 5053 | if (WARN_ON(ecc->mode != NAND_ECC_SOFT)) |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5054 | return -EINVAL; |
| 5055 | |
| 5056 | switch (ecc->algo) { |
| 5057 | case NAND_ECC_HAMMING: |
| 5058 | ecc->calculate = nand_calculate_ecc; |
| 5059 | ecc->correct = nand_correct_data; |
| 5060 | ecc->read_page = nand_read_page_swecc; |
| 5061 | ecc->read_subpage = nand_read_subpage; |
| 5062 | ecc->write_page = nand_write_page_swecc; |
| 5063 | ecc->read_page_raw = nand_read_page_raw; |
| 5064 | ecc->write_page_raw = nand_write_page_raw; |
| 5065 | ecc->read_oob = nand_read_oob_std; |
| 5066 | ecc->write_oob = nand_write_oob_std; |
| 5067 | if (!ecc->size) |
| 5068 | ecc->size = 256; |
| 5069 | ecc->bytes = 3; |
| 5070 | ecc->strength = 1; |
Boris Brezillon | 309600c | 2018-09-04 16:23:28 +0200 | [diff] [blame] | 5071 | |
| 5072 | if (IS_ENABLED(CONFIG_MTD_NAND_ECC_SMC)) |
| 5073 | ecc->options |= NAND_ECC_SOFT_HAMMING_SM_ORDER; |
| 5074 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5075 | return 0; |
| 5076 | case NAND_ECC_BCH: |
| 5077 | if (!mtd_nand_has_bch()) { |
| 5078 | WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n"); |
| 5079 | return -EINVAL; |
| 5080 | } |
| 5081 | ecc->calculate = nand_bch_calculate_ecc; |
| 5082 | ecc->correct = nand_bch_correct_data; |
| 5083 | ecc->read_page = nand_read_page_swecc; |
| 5084 | ecc->read_subpage = nand_read_subpage; |
| 5085 | ecc->write_page = nand_write_page_swecc; |
| 5086 | ecc->read_page_raw = nand_read_page_raw; |
| 5087 | ecc->write_page_raw = nand_write_page_raw; |
| 5088 | ecc->read_oob = nand_read_oob_std; |
| 5089 | ecc->write_oob = nand_write_oob_std; |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 5090 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5091 | /* |
| 5092 | * Board driver should supply ecc.size and ecc.strength |
| 5093 | * values to select how many bits are correctable. |
| 5094 | * Otherwise, default to 4 bits for large page devices. |
| 5095 | */ |
| 5096 | if (!ecc->size && (mtd->oobsize >= 64)) { |
| 5097 | ecc->size = 512; |
| 5098 | ecc->strength = 4; |
| 5099 | } |
| 5100 | |
| 5101 | /* |
| 5102 | * if no ecc placement scheme was provided pickup the default |
| 5103 | * large page one. |
| 5104 | */ |
| 5105 | if (!mtd->ooblayout) { |
| 5106 | /* handle large page devices only */ |
| 5107 | if (mtd->oobsize < 64) { |
| 5108 | WARN(1, "OOB layout is required when using software BCH on small pages\n"); |
| 5109 | return -EINVAL; |
| 5110 | } |
| 5111 | |
| 5112 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops); |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 5113 | |
| 5114 | } |
| 5115 | |
| 5116 | /* |
| 5117 | * We can only maximize ECC config when the default layout is |
| 5118 | * used, otherwise we don't know how many bytes can really be |
| 5119 | * used. |
| 5120 | */ |
| 5121 | if (mtd->ooblayout == &nand_ooblayout_lp_ops && |
| 5122 | ecc->options & NAND_ECC_MAXIMIZE) { |
| 5123 | int steps, bytes; |
| 5124 | |
| 5125 | /* Always prefer 1k blocks over 512bytes ones */ |
| 5126 | ecc->size = 1024; |
| 5127 | steps = mtd->writesize / ecc->size; |
| 5128 | |
| 5129 | /* Reserve 2 bytes for the BBM */ |
| 5130 | bytes = (mtd->oobsize - 2) / steps; |
| 5131 | ecc->strength = bytes * 8 / fls(8 * ecc->size); |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5132 | } |
| 5133 | |
| 5134 | /* See nand_bch_init() for details. */ |
| 5135 | ecc->bytes = 0; |
| 5136 | ecc->priv = nand_bch_init(mtd); |
| 5137 | if (!ecc->priv) { |
| 5138 | WARN(1, "BCH ECC initialization failed!\n"); |
| 5139 | return -EINVAL; |
| 5140 | } |
| 5141 | return 0; |
| 5142 | default: |
| 5143 | WARN(1, "Unsupported ECC algorithm!\n"); |
| 5144 | return -EINVAL; |
| 5145 | } |
| 5146 | } |
| 5147 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5148 | /** |
| 5149 | * nand_check_ecc_caps - check the sanity of preset ECC settings |
| 5150 | * @chip: nand chip info structure |
| 5151 | * @caps: ECC caps info structure |
| 5152 | * @oobavail: OOB size that the ECC engine can use |
| 5153 | * |
| 5154 | * When ECC step size and strength are already set, check if they are supported |
| 5155 | * by the controller and the calculated ECC bytes fit within the chip's OOB. |
| 5156 | * On success, the calculated ECC bytes is set. |
| 5157 | */ |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 5158 | static int |
| 5159 | nand_check_ecc_caps(struct nand_chip *chip, |
| 5160 | const struct nand_ecc_caps *caps, int oobavail) |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5161 | { |
| 5162 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 5163 | const struct nand_ecc_step_info *stepinfo; |
| 5164 | int preset_step = chip->ecc.size; |
| 5165 | int preset_strength = chip->ecc.strength; |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 5166 | int ecc_bytes, nsteps = mtd->writesize / preset_step; |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5167 | int i, j; |
| 5168 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5169 | for (i = 0; i < caps->nstepinfos; i++) { |
| 5170 | stepinfo = &caps->stepinfos[i]; |
| 5171 | |
| 5172 | if (stepinfo->stepsize != preset_step) |
| 5173 | continue; |
| 5174 | |
| 5175 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 5176 | if (stepinfo->strengths[j] != preset_strength) |
| 5177 | continue; |
| 5178 | |
| 5179 | ecc_bytes = caps->calc_ecc_bytes(preset_step, |
| 5180 | preset_strength); |
| 5181 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 5182 | return ecc_bytes; |
| 5183 | |
| 5184 | if (ecc_bytes * nsteps > oobavail) { |
| 5185 | pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB", |
| 5186 | preset_step, preset_strength); |
| 5187 | return -ENOSPC; |
| 5188 | } |
| 5189 | |
| 5190 | chip->ecc.bytes = ecc_bytes; |
| 5191 | |
| 5192 | return 0; |
| 5193 | } |
| 5194 | } |
| 5195 | |
| 5196 | pr_err("ECC (step, strength) = (%d, %d) not supported on this controller", |
| 5197 | preset_step, preset_strength); |
| 5198 | |
| 5199 | return -ENOTSUPP; |
| 5200 | } |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5201 | |
| 5202 | /** |
| 5203 | * nand_match_ecc_req - meet the chip's requirement with least ECC bytes |
| 5204 | * @chip: nand chip info structure |
| 5205 | * @caps: ECC engine caps info structure |
| 5206 | * @oobavail: OOB size that the ECC engine can use |
| 5207 | * |
| 5208 | * If a chip's ECC requirement is provided, try to meet it with the least |
| 5209 | * number of ECC bytes (i.e. with the largest number of OOB-free bytes). |
| 5210 | * On success, the chosen ECC settings are set. |
| 5211 | */ |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 5212 | static int |
| 5213 | nand_match_ecc_req(struct nand_chip *chip, |
| 5214 | const struct nand_ecc_caps *caps, int oobavail) |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5215 | { |
| 5216 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 5217 | const struct nand_ecc_step_info *stepinfo; |
| 5218 | int req_step = chip->ecc_step_ds; |
| 5219 | int req_strength = chip->ecc_strength_ds; |
| 5220 | int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total; |
| 5221 | int best_step, best_strength, best_ecc_bytes; |
| 5222 | int best_ecc_bytes_total = INT_MAX; |
| 5223 | int i, j; |
| 5224 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5225 | /* No information provided by the NAND chip */ |
| 5226 | if (!req_step || !req_strength) |
| 5227 | return -ENOTSUPP; |
| 5228 | |
| 5229 | /* number of correctable bits the chip requires in a page */ |
| 5230 | req_corr = mtd->writesize / req_step * req_strength; |
| 5231 | |
| 5232 | for (i = 0; i < caps->nstepinfos; i++) { |
| 5233 | stepinfo = &caps->stepinfos[i]; |
| 5234 | step_size = stepinfo->stepsize; |
| 5235 | |
| 5236 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 5237 | strength = stepinfo->strengths[j]; |
| 5238 | |
| 5239 | /* |
| 5240 | * If both step size and strength are smaller than the |
| 5241 | * chip's requirement, it is not easy to compare the |
| 5242 | * resulted reliability. |
| 5243 | */ |
| 5244 | if (step_size < req_step && strength < req_strength) |
| 5245 | continue; |
| 5246 | |
| 5247 | if (mtd->writesize % step_size) |
| 5248 | continue; |
| 5249 | |
| 5250 | nsteps = mtd->writesize / step_size; |
| 5251 | |
| 5252 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 5253 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 5254 | continue; |
| 5255 | ecc_bytes_total = ecc_bytes * nsteps; |
| 5256 | |
| 5257 | if (ecc_bytes_total > oobavail || |
| 5258 | strength * nsteps < req_corr) |
| 5259 | continue; |
| 5260 | |
| 5261 | /* |
| 5262 | * We assume the best is to meet the chip's requrement |
| 5263 | * with the least number of ECC bytes. |
| 5264 | */ |
| 5265 | if (ecc_bytes_total < best_ecc_bytes_total) { |
| 5266 | best_ecc_bytes_total = ecc_bytes_total; |
| 5267 | best_step = step_size; |
| 5268 | best_strength = strength; |
| 5269 | best_ecc_bytes = ecc_bytes; |
| 5270 | } |
| 5271 | } |
| 5272 | } |
| 5273 | |
| 5274 | if (best_ecc_bytes_total == INT_MAX) |
| 5275 | return -ENOTSUPP; |
| 5276 | |
| 5277 | chip->ecc.size = best_step; |
| 5278 | chip->ecc.strength = best_strength; |
| 5279 | chip->ecc.bytes = best_ecc_bytes; |
| 5280 | |
| 5281 | return 0; |
| 5282 | } |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5283 | |
| 5284 | /** |
| 5285 | * nand_maximize_ecc - choose the max ECC strength available |
| 5286 | * @chip: nand chip info structure |
| 5287 | * @caps: ECC engine caps info structure |
| 5288 | * @oobavail: OOB size that the ECC engine can use |
| 5289 | * |
| 5290 | * Choose the max ECC strength that is supported on the controller, and can fit |
| 5291 | * within the chip's OOB. On success, the chosen ECC settings are set. |
| 5292 | */ |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 5293 | static int |
| 5294 | nand_maximize_ecc(struct nand_chip *chip, |
| 5295 | const struct nand_ecc_caps *caps, int oobavail) |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5296 | { |
| 5297 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 5298 | const struct nand_ecc_step_info *stepinfo; |
| 5299 | int step_size, strength, nsteps, ecc_bytes, corr; |
| 5300 | int best_corr = 0; |
| 5301 | int best_step = 0; |
| 5302 | int best_strength, best_ecc_bytes; |
| 5303 | int i, j; |
| 5304 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5305 | for (i = 0; i < caps->nstepinfos; i++) { |
| 5306 | stepinfo = &caps->stepinfos[i]; |
| 5307 | step_size = stepinfo->stepsize; |
| 5308 | |
| 5309 | /* If chip->ecc.size is already set, respect it */ |
| 5310 | if (chip->ecc.size && step_size != chip->ecc.size) |
| 5311 | continue; |
| 5312 | |
| 5313 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 5314 | strength = stepinfo->strengths[j]; |
| 5315 | |
| 5316 | if (mtd->writesize % step_size) |
| 5317 | continue; |
| 5318 | |
| 5319 | nsteps = mtd->writesize / step_size; |
| 5320 | |
| 5321 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 5322 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 5323 | continue; |
| 5324 | |
| 5325 | if (ecc_bytes * nsteps > oobavail) |
| 5326 | continue; |
| 5327 | |
| 5328 | corr = strength * nsteps; |
| 5329 | |
| 5330 | /* |
| 5331 | * If the number of correctable bits is the same, |
| 5332 | * bigger step_size has more reliability. |
| 5333 | */ |
| 5334 | if (corr > best_corr || |
| 5335 | (corr == best_corr && step_size > best_step)) { |
| 5336 | best_corr = corr; |
| 5337 | best_step = step_size; |
| 5338 | best_strength = strength; |
| 5339 | best_ecc_bytes = ecc_bytes; |
| 5340 | } |
| 5341 | } |
| 5342 | } |
| 5343 | |
| 5344 | if (!best_corr) |
| 5345 | return -ENOTSUPP; |
| 5346 | |
| 5347 | chip->ecc.size = best_step; |
| 5348 | chip->ecc.strength = best_strength; |
| 5349 | chip->ecc.bytes = best_ecc_bytes; |
| 5350 | |
| 5351 | return 0; |
| 5352 | } |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5353 | |
Abhishek Sahu | 181ace9 | 2018-06-20 12:57:28 +0530 | [diff] [blame] | 5354 | /** |
| 5355 | * nand_ecc_choose_conf - Set the ECC strength and ECC step size |
| 5356 | * @chip: nand chip info structure |
| 5357 | * @caps: ECC engine caps info structure |
| 5358 | * @oobavail: OOB size that the ECC engine can use |
| 5359 | * |
| 5360 | * Choose the ECC configuration according to following logic |
| 5361 | * |
| 5362 | * 1. If both ECC step size and ECC strength are already set (usually by DT) |
| 5363 | * then check if it is supported by this controller. |
| 5364 | * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength. |
| 5365 | * 3. Otherwise, try to match the ECC step size and ECC strength closest |
| 5366 | * to the chip's requirement. If available OOB size can't fit the chip |
| 5367 | * requirement then fallback to the maximum ECC step size and ECC strength. |
| 5368 | * |
| 5369 | * On success, the chosen ECC settings are set. |
| 5370 | */ |
| 5371 | int nand_ecc_choose_conf(struct nand_chip *chip, |
| 5372 | const struct nand_ecc_caps *caps, int oobavail) |
| 5373 | { |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 5374 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 5375 | |
| 5376 | if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize)) |
| 5377 | return -EINVAL; |
| 5378 | |
Abhishek Sahu | 181ace9 | 2018-06-20 12:57:28 +0530 | [diff] [blame] | 5379 | if (chip->ecc.size && chip->ecc.strength) |
| 5380 | return nand_check_ecc_caps(chip, caps, oobavail); |
| 5381 | |
| 5382 | if (chip->ecc.options & NAND_ECC_MAXIMIZE) |
| 5383 | return nand_maximize_ecc(chip, caps, oobavail); |
| 5384 | |
| 5385 | if (!nand_match_ecc_req(chip, caps, oobavail)) |
| 5386 | return 0; |
| 5387 | |
| 5388 | return nand_maximize_ecc(chip, caps, oobavail); |
| 5389 | } |
| 5390 | EXPORT_SYMBOL_GPL(nand_ecc_choose_conf); |
| 5391 | |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5392 | /* |
| 5393 | * Check if the chip configuration meet the datasheet requirements. |
| 5394 | |
| 5395 | * If our configuration corrects A bits per B bytes and the minimum |
| 5396 | * required correction level is X bits per Y bytes, then we must ensure |
| 5397 | * both of the following are true: |
| 5398 | * |
| 5399 | * (1) A / B >= X / Y |
| 5400 | * (2) A >= X |
| 5401 | * |
| 5402 | * Requirement (1) ensures we can correct for the required bitflip density. |
| 5403 | * Requirement (2) ensures we can correct even when all bitflips are clumped |
| 5404 | * in the same sector. |
| 5405 | */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 5406 | static bool nand_ecc_strength_good(struct nand_chip *chip) |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5407 | { |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 5408 | struct mtd_info *mtd = nand_to_mtd(chip); |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5409 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 5410 | int corr, ds_corr; |
| 5411 | |
| 5412 | if (ecc->size == 0 || chip->ecc_step_ds == 0) |
| 5413 | /* Not enough information */ |
| 5414 | return true; |
| 5415 | |
| 5416 | /* |
| 5417 | * We get the number of corrected bits per page to compare |
| 5418 | * the correction density. |
| 5419 | */ |
| 5420 | corr = (mtd->writesize * ecc->strength) / ecc->size; |
| 5421 | ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds; |
| 5422 | |
| 5423 | return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds; |
| 5424 | } |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5425 | |
| 5426 | /** |
Miquel Raynal | 98732da | 2018-07-25 15:31:50 +0200 | [diff] [blame] | 5427 | * nand_scan_tail - Scan for the NAND device |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5428 | * @chip: NAND chip object |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5429 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5430 | * This is the second phase of the normal nand_scan() function. It fills out |
| 5431 | * all the uninitialized function pointers with the defaults and scans for a |
| 5432 | * bad block table if appropriate. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5433 | */ |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5434 | static int nand_scan_tail(struct nand_chip *chip) |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5435 | { |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5436 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5437 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5438 | int ret, i; |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5439 | |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 5440 | /* 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] | 5441 | if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) && |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 5442 | !(chip->bbt_options & NAND_BBT_USE_FLASH))) { |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5443 | return -EINVAL; |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 5444 | } |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 5445 | |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 5446 | chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL); |
Boris Brezillon | aeb93af | 2017-12-05 12:09:29 +0100 | [diff] [blame] | 5447 | if (!chip->data_buf) |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5448 | return -ENOMEM; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 5449 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5450 | /* |
| 5451 | * FIXME: some NAND manufacturer drivers expect the first die to be |
| 5452 | * selected when manufacturer->init() is called. They should be fixed |
| 5453 | * to explictly select the relevant die when interacting with the NAND |
| 5454 | * chip. |
| 5455 | */ |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 5456 | chip->select_chip(chip, 0); |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5457 | ret = nand_manufacturer_init(chip); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 5458 | chip->select_chip(chip, -1); |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5459 | if (ret) |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 5460 | goto err_free_buf; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5461 | |
David Woodhouse | 7dcdcbef | 2006-10-21 17:09:53 +0100 | [diff] [blame] | 5462 | /* Set the internal oob buffer location, just after the page data */ |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 5463 | chip->oob_poi = chip->data_buf + mtd->writesize; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5464 | |
| 5465 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5466 | * If no default placement scheme is given, select an appropriate one. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5467 | */ |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5468 | if (!mtd->ooblayout && |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 5469 | !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5470 | switch (mtd->oobsize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5471 | case 8: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5472 | case 16: |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 5473 | mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | break; |
| 5475 | case 64: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 5476 | case 128: |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 5477 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops); |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 5478 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5479 | default: |
Miquel Raynal | 882fd15 | 2017-08-26 17:19:15 +0200 | [diff] [blame] | 5480 | /* |
| 5481 | * Expose the whole OOB area to users if ECC_NONE |
| 5482 | * is passed. We could do that for all kind of |
| 5483 | * ->oobsize, but we must keep the old large/small |
| 5484 | * page with ECC layout when ->oobsize <= 128 for |
| 5485 | * compatibility reasons. |
| 5486 | */ |
| 5487 | if (ecc->mode == NAND_ECC_NONE) { |
| 5488 | mtd_set_ooblayout(mtd, |
| 5489 | &nand_ooblayout_lp_ops); |
| 5490 | break; |
| 5491 | } |
| 5492 | |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5493 | WARN(1, "No oob scheme defined for oobsize %d\n", |
| 5494 | mtd->oobsize); |
| 5495 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5496 | goto err_nand_manuf_cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | } |
| 5498 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5499 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5500 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5501 | * Check ECC mode, default to software if 3byte/512byte hardware ECC is |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5502 | * selected and we have 256 byte pagesize fallback to software ECC |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5503 | */ |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 5504 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5505 | switch (ecc->mode) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 5506 | case NAND_ECC_HW_OOB_FIRST: |
| 5507 | /* Similar to NAND_ECC_HW, but a separate read_page handle */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5508 | if (!ecc->calculate || !ecc->correct || !ecc->hwctl) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5509 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 5510 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5511 | goto err_nand_manuf_cleanup; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 5512 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5513 | if (!ecc->read_page) |
| 5514 | ecc->read_page = nand_read_page_hwecc_oob_first; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 5515 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5516 | case NAND_ECC_HW: |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5517 | /* Use standard hwecc read page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5518 | if (!ecc->read_page) |
| 5519 | ecc->read_page = nand_read_page_hwecc; |
| 5520 | if (!ecc->write_page) |
| 5521 | ecc->write_page = nand_write_page_hwecc; |
| 5522 | if (!ecc->read_page_raw) |
| 5523 | ecc->read_page_raw = nand_read_page_raw; |
| 5524 | if (!ecc->write_page_raw) |
| 5525 | ecc->write_page_raw = nand_write_page_raw; |
| 5526 | if (!ecc->read_oob) |
| 5527 | ecc->read_oob = nand_read_oob_std; |
| 5528 | if (!ecc->write_oob) |
| 5529 | ecc->write_oob = nand_write_oob_std; |
| 5530 | if (!ecc->read_subpage) |
| 5531 | ecc->read_subpage = nand_read_subpage; |
Helmut Schaa | 44991b3 | 2014-04-09 11:13:24 +0200 | [diff] [blame] | 5532 | if (!ecc->write_subpage && ecc->hwctl && ecc->calculate) |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5533 | ecc->write_subpage = nand_write_subpage_hwecc; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 5534 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5535 | case NAND_ECC_HW_SYNDROME: |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5536 | if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) && |
| 5537 | (!ecc->read_page || |
| 5538 | ecc->read_page == nand_read_page_hwecc || |
| 5539 | !ecc->write_page || |
| 5540 | ecc->write_page == nand_write_page_hwecc)) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5541 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 5542 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5543 | goto err_nand_manuf_cleanup; |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5544 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5545 | /* Use standard syndrome read/write page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5546 | if (!ecc->read_page) |
| 5547 | ecc->read_page = nand_read_page_syndrome; |
| 5548 | if (!ecc->write_page) |
| 5549 | ecc->write_page = nand_write_page_syndrome; |
| 5550 | if (!ecc->read_page_raw) |
| 5551 | ecc->read_page_raw = nand_read_page_raw_syndrome; |
| 5552 | if (!ecc->write_page_raw) |
| 5553 | ecc->write_page_raw = nand_write_page_raw_syndrome; |
| 5554 | if (!ecc->read_oob) |
| 5555 | ecc->read_oob = nand_read_oob_syndrome; |
| 5556 | if (!ecc->write_oob) |
| 5557 | ecc->write_oob = nand_write_oob_syndrome; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 5558 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5559 | if (mtd->writesize >= ecc->size) { |
| 5560 | if (!ecc->strength) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5561 | WARN(1, "Driver must set ecc.strength when using hardware ECC\n"); |
| 5562 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5563 | goto err_nand_manuf_cleanup; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 5564 | } |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5565 | break; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 5566 | } |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 5567 | pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n", |
| 5568 | ecc->size, mtd->writesize); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5569 | ecc->mode = NAND_ECC_SOFT; |
Rafał Miłecki | e9d4fae | 2016-04-17 22:53:02 +0200 | [diff] [blame] | 5570 | ecc->algo = NAND_ECC_HAMMING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5571 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 5572 | case NAND_ECC_SOFT: |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 5573 | ret = nand_set_ecc_soft_ops(chip); |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5574 | if (ret) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5575 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5576 | goto err_nand_manuf_cleanup; |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 5577 | } |
| 5578 | break; |
| 5579 | |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 5580 | case NAND_ECC_ON_DIE: |
| 5581 | if (!ecc->read_page || !ecc->write_page) { |
| 5582 | WARN(1, "No ECC functions supplied; on-die ECC not possible\n"); |
| 5583 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5584 | goto err_nand_manuf_cleanup; |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 5585 | } |
| 5586 | if (!ecc->read_oob) |
| 5587 | ecc->read_oob = nand_read_oob_std; |
| 5588 | if (!ecc->write_oob) |
| 5589 | ecc->write_oob = nand_write_oob_std; |
| 5590 | break; |
| 5591 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5592 | case NAND_ECC_NONE: |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 5593 | 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] | 5594 | ecc->read_page = nand_read_page_raw; |
| 5595 | ecc->write_page = nand_write_page_raw; |
| 5596 | ecc->read_oob = nand_read_oob_std; |
| 5597 | ecc->read_page_raw = nand_read_page_raw; |
| 5598 | ecc->write_page_raw = nand_write_page_raw; |
| 5599 | ecc->write_oob = nand_write_oob_std; |
| 5600 | ecc->size = mtd->writesize; |
| 5601 | ecc->bytes = 0; |
| 5602 | ecc->strength = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5603 | break; |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 5604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5605 | default: |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5606 | WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode); |
| 5607 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5608 | goto err_nand_manuf_cleanup; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5609 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5610 | |
Boris Brezillon | aeb93af | 2017-12-05 12:09:29 +0100 | [diff] [blame] | 5611 | if (ecc->correct || ecc->calculate) { |
| 5612 | ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL); |
| 5613 | ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL); |
| 5614 | if (!ecc->calc_buf || !ecc->code_buf) { |
| 5615 | ret = -ENOMEM; |
| 5616 | goto err_nand_manuf_cleanup; |
| 5617 | } |
| 5618 | } |
| 5619 | |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 5620 | /* For many systems, the standard OOB write also works for raw */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5621 | if (!ecc->read_oob_raw) |
| 5622 | ecc->read_oob_raw = ecc->read_oob; |
| 5623 | if (!ecc->write_oob_raw) |
| 5624 | ecc->write_oob_raw = ecc->write_oob; |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 5625 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 5626 | /* propagate ecc info to mtd_info */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 5627 | mtd->ecc_strength = ecc->strength; |
| 5628 | mtd->ecc_step_size = ecc->size; |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 5629 | |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 5630 | /* |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5631 | * 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] | 5632 | * mode. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5633 | */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5634 | ecc->steps = mtd->writesize / ecc->size; |
| 5635 | if (ecc->steps * ecc->size != mtd->writesize) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5636 | WARN(1, "Invalid ECC parameters\n"); |
| 5637 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5638 | goto err_nand_manuf_cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5639 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5640 | ecc->total = ecc->steps * ecc->bytes; |
Masahiro Yamada | 79e0348 | 2017-05-25 13:50:20 +0900 | [diff] [blame] | 5641 | if (ecc->total > mtd->oobsize) { |
| 5642 | WARN(1, "Total number of ECC bytes exceeded oobsize\n"); |
| 5643 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5644 | goto err_nand_manuf_cleanup; |
Masahiro Yamada | 79e0348 | 2017-05-25 13:50:20 +0900 | [diff] [blame] | 5645 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5646 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 5647 | /* |
| 5648 | * The number of bytes available for a client to place data into |
| 5649 | * the out of band area. |
| 5650 | */ |
| 5651 | ret = mtd_ooblayout_count_freebytes(mtd); |
| 5652 | if (ret < 0) |
| 5653 | ret = 0; |
| 5654 | |
| 5655 | mtd->oobavail = ret; |
| 5656 | |
| 5657 | /* ECC sanity check: warn if it's too weak */ |
Boris Brezillon | 0813621 | 2018-11-11 08:55:03 +0100 | [diff] [blame^] | 5658 | if (!nand_ecc_strength_good(chip)) |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 5659 | pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n", |
| 5660 | mtd->name); |
| 5661 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5662 | /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */ |
Huang Shijie | 1d0ed69 | 2013-09-25 14:58:10 +0800 | [diff] [blame] | 5663 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) { |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 5664 | switch (ecc->steps) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 5665 | case 2: |
| 5666 | mtd->subpage_sft = 1; |
| 5667 | break; |
| 5668 | case 4: |
| 5669 | case 8: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 5670 | case 16: |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 5671 | mtd->subpage_sft = 2; |
| 5672 | break; |
| 5673 | } |
| 5674 | } |
| 5675 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; |
| 5676 | |
Thomas Gleixner | 04bbd0e | 2006-05-25 09:45:29 +0200 | [diff] [blame] | 5677 | /* Initialize state */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5678 | chip->state = FL_READY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5680 | /* Invalidate the pagebuffer reference */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5681 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 5683 | /* Large page NAND with SOFT_ECC should support subpage reads */ |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 5684 | switch (ecc->mode) { |
| 5685 | case NAND_ECC_SOFT: |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 5686 | if (chip->page_shift > 9) |
| 5687 | chip->options |= NAND_SUBPAGE_READ; |
| 5688 | break; |
| 5689 | |
| 5690 | default: |
| 5691 | break; |
| 5692 | } |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 5693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5694 | /* Fill in remaining MTD driver data */ |
Huang Shijie | 963d1c2 | 2013-09-25 14:58:21 +0800 | [diff] [blame] | 5695 | mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH; |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 5696 | mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM : |
| 5697 | MTD_CAP_NANDFLASH; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 5698 | mtd->_erase = nand_erase; |
| 5699 | mtd->_point = NULL; |
| 5700 | mtd->_unpoint = NULL; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 5701 | mtd->_panic_write = panic_nand_write; |
| 5702 | mtd->_read_oob = nand_read_oob; |
| 5703 | mtd->_write_oob = nand_write_oob; |
| 5704 | mtd->_sync = nand_sync; |
| 5705 | mtd->_lock = NULL; |
| 5706 | mtd->_unlock = NULL; |
| 5707 | mtd->_suspend = nand_suspend; |
| 5708 | mtd->_resume = nand_resume; |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 5709 | mtd->_reboot = nand_shutdown; |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 5710 | mtd->_block_isreserved = nand_block_isreserved; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 5711 | mtd->_block_isbad = nand_block_isbad; |
| 5712 | mtd->_block_markbad = nand_block_markbad; |
Zach Brown | 5671842 | 2017-01-10 13:30:20 -0600 | [diff] [blame] | 5713 | mtd->_max_bad_blocks = nand_max_bad_blocks; |
Anatolij Gustschin | cbcab65 | 2010-12-16 23:42:16 +0100 | [diff] [blame] | 5714 | mtd->writebufsize = mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5715 | |
Shmulik Ladkani | ea3b2ea | 2012-06-08 18:29:06 +0300 | [diff] [blame] | 5716 | /* |
| 5717 | * Initialize bitflip_threshold to its default prior scan_bbt() call. |
| 5718 | * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be |
| 5719 | * properly set. |
| 5720 | */ |
| 5721 | if (!mtd->bitflip_threshold) |
Brian Norris | 240181f | 2015-01-12 12:51:29 -0800 | [diff] [blame] | 5722 | mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5723 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5724 | /* Initialize the ->data_interface field. */ |
| 5725 | ret = nand_init_data_interface(chip); |
| 5726 | if (ret) |
| 5727 | goto err_nand_manuf_cleanup; |
| 5728 | |
| 5729 | /* Enter fastest possible mode on all dies. */ |
| 5730 | for (i = 0; i < chip->numchips; i++) { |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5731 | ret = nand_setup_data_interface(chip, i); |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5732 | if (ret) |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 5733 | goto err_nand_manuf_cleanup; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5734 | } |
| 5735 | |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 5736 | /* Check, if we should skip the bad block table scan */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5737 | if (chip->options & NAND_SKIP_BBTSCAN) |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 5738 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5739 | |
| 5740 | /* Build bad block table */ |
Boris Brezillon | e80eba7 | 2018-07-05 12:27:31 +0200 | [diff] [blame] | 5741 | ret = nand_create_bbt(chip); |
Brian Norris | 44d4182 | 2017-05-01 17:04:50 -0700 | [diff] [blame] | 5742 | if (ret) |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 5743 | goto err_nand_manuf_cleanup; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5744 | |
Brian Norris | 44d4182 | 2017-05-01 17:04:50 -0700 | [diff] [blame] | 5745 | return 0; |
| 5746 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 5747 | |
| 5748 | err_nand_manuf_cleanup: |
| 5749 | nand_manufacturer_cleanup(chip); |
| 5750 | |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 5751 | err_free_buf: |
| 5752 | kfree(chip->data_buf); |
| 5753 | kfree(ecc->code_buf); |
| 5754 | kfree(ecc->calc_buf); |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 5755 | |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 5756 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5757 | } |
| 5758 | |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5759 | static int nand_attach(struct nand_chip *chip) |
| 5760 | { |
| 5761 | if (chip->controller->ops && chip->controller->ops->attach_chip) |
| 5762 | return chip->controller->ops->attach_chip(chip); |
| 5763 | |
| 5764 | return 0; |
| 5765 | } |
| 5766 | |
| 5767 | static void nand_detach(struct nand_chip *chip) |
| 5768 | { |
| 5769 | if (chip->controller->ops && chip->controller->ops->detach_chip) |
| 5770 | chip->controller->ops->detach_chip(chip); |
| 5771 | } |
| 5772 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5773 | /** |
Miquel Raynal | 256c4fc | 2018-04-22 18:02:30 +0200 | [diff] [blame] | 5774 | * nand_scan_with_ids - [NAND Interface] Scan for the NAND device |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5775 | * @chip: NAND chip object |
Boris Brezillon | 800342d | 2018-08-04 22:59:23 +0200 | [diff] [blame] | 5776 | * @maxchips: number of chips to scan for. |
Miquel Raynal | 256c4fc | 2018-04-22 18:02:30 +0200 | [diff] [blame] | 5777 | * @ids: optional flash IDs table |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5778 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5779 | * This fills out all the uninitialized function pointers with the defaults. |
| 5780 | * 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] | 5781 | * appropriate values. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5782 | */ |
Boris Brezillon | 871a407 | 2018-08-04 22:59:22 +0200 | [diff] [blame] | 5783 | int nand_scan_with_ids(struct nand_chip *chip, unsigned int maxchips, |
Miquel Raynal | 256c4fc | 2018-04-22 18:02:30 +0200 | [diff] [blame] | 5784 | struct nand_flash_dev *ids) |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5785 | { |
| 5786 | int ret; |
| 5787 | |
Boris Brezillon | 800342d | 2018-08-04 22:59:23 +0200 | [diff] [blame] | 5788 | if (!maxchips) |
| 5789 | return -EINVAL; |
| 5790 | |
| 5791 | ret = nand_scan_ident(chip, maxchips, ids); |
| 5792 | if (ret) |
| 5793 | return ret; |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5794 | |
| 5795 | ret = nand_attach(chip); |
| 5796 | if (ret) |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5797 | goto cleanup_ident; |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5798 | |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5799 | ret = nand_scan_tail(chip); |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5800 | if (ret) |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5801 | goto detach_chip; |
| 5802 | |
| 5803 | return 0; |
| 5804 | |
| 5805 | detach_chip: |
| 5806 | nand_detach(chip); |
| 5807 | cleanup_ident: |
| 5808 | nand_scan_ident_cleanup(chip); |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5809 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5810 | return ret; |
| 5811 | } |
Miquel Raynal | 256c4fc | 2018-04-22 18:02:30 +0200 | [diff] [blame] | 5812 | EXPORT_SYMBOL(nand_scan_with_ids); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5814 | /** |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5815 | * nand_cleanup - [NAND Interface] Free resources held by the NAND device |
| 5816 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5817 | */ |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5818 | void nand_cleanup(struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5819 | { |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 5820 | if (chip->ecc.mode == NAND_ECC_SOFT && |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5821 | chip->ecc.algo == NAND_ECC_BCH) |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 5822 | nand_bch_free((struct nand_bch_control *)chip->ecc.priv); |
| 5823 | |
Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 5824 | /* Free bad block table memory */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5825 | kfree(chip->bbt); |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 5826 | kfree(chip->data_buf); |
| 5827 | kfree(chip->ecc.code_buf); |
| 5828 | kfree(chip->ecc.calc_buf); |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 5829 | |
| 5830 | /* Free bad block descriptor memory */ |
| 5831 | if (chip->badblock_pattern && chip->badblock_pattern->options |
| 5832 | & NAND_BBT_DYNAMICSTRUCT) |
| 5833 | kfree(chip->badblock_pattern); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5834 | |
| 5835 | /* Free manufacturer priv data. */ |
| 5836 | nand_manufacturer_cleanup(chip); |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5837 | |
| 5838 | /* Free controller specific allocations after chip identification */ |
| 5839 | nand_detach(chip); |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5840 | |
| 5841 | /* Free identification phase allocations */ |
| 5842 | nand_scan_ident_cleanup(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5843 | } |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 5844 | |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5845 | EXPORT_SYMBOL_GPL(nand_cleanup); |
| 5846 | |
| 5847 | /** |
| 5848 | * nand_release - [NAND Interface] Unregister the MTD device and free resources |
| 5849 | * held by the NAND device |
Boris Brezillon | 59ac276 | 2018-09-06 14:05:15 +0200 | [diff] [blame] | 5850 | * @chip: NAND chip object |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5851 | */ |
Boris Brezillon | 59ac276 | 2018-09-06 14:05:15 +0200 | [diff] [blame] | 5852 | void nand_release(struct nand_chip *chip) |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5853 | { |
Boris Brezillon | 59ac276 | 2018-09-06 14:05:15 +0200 | [diff] [blame] | 5854 | mtd_device_unregister(nand_to_mtd(chip)); |
| 5855 | nand_cleanup(chip); |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5856 | } |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5857 | EXPORT_SYMBOL_GPL(nand_release); |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 5858 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5859 | MODULE_LICENSE("GPL"); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 5860 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>"); |
| 5861 | MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>"); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5862 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |