Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Overview: |
| 3 | * This is the generic MTD driver for NAND flash devices. It should be |
| 4 | * capable of working with almost all NAND chips currently available. |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Additional technical information is available on |
maximilian attems | 8b2b403 | 2007-07-28 13:07:16 +0200 | [diff] [blame] | 7 | * http://www.linux-mtd.infradead.org/doc/nand.html |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 10 | * 2002-2006 Thomas Gleixner (tglx@linutronix.de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 12 | * Credits: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 13 | * David Woodhouse for adding multichip support |
| 14 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * Aleph One Ltd. and Toby Churchill Ltd. for supporting the |
| 16 | * rework for 2K page size chips |
| 17 | * |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 18 | * TODO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * Enable cached programming for 2k page size chips |
| 20 | * Check, if mtd->ecctype should be set to MTD_ECC_HW |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 21 | * if we have HW ECC support. |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 22 | * BBT table is not serialized, has to be fixed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * This program is free software; you can redistribute it and/or modify |
| 25 | * it under the terms of the GNU General Public License version 2 as |
| 26 | * published by the Free Software Foundation. |
| 27 | * |
| 28 | */ |
| 29 | |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 30 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 31 | |
David Woodhouse | 552d920 | 2006-05-14 01:20:46 +0100 | [diff] [blame] | 32 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/delay.h> |
| 34 | #include <linux/errno.h> |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 35 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/sched.h> |
| 37 | #include <linux/slab.h> |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 38 | #include <linux/mm.h> |
Ingo Molnar | 38b8d20 | 2017-02-08 18:51:31 +0100 | [diff] [blame] | 39 | #include <linux/nmi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/types.h> |
| 41 | #include <linux/mtd/mtd.h> |
Boris Brezillon | d4092d7 | 2017-08-04 17:29:10 +0200 | [diff] [blame] | 42 | #include <linux/mtd/rawnand.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/mtd/nand_ecc.h> |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 44 | #include <linux/mtd/nand_bch.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/interrupt.h> |
| 46 | #include <linux/bitops.h> |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 47 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/mtd/partitions.h> |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 49 | #include <linux/of.h> |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 50 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 51 | static int nand_get_device(struct mtd_info *mtd, int new_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 53 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 54 | struct mtd_oob_ops *ops); |
| 55 | |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 56 | /* Define default oob placement schemes for large and small page devices */ |
| 57 | static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section, |
| 58 | struct mtd_oob_region *oobregion) |
| 59 | { |
| 60 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 61 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 62 | |
| 63 | if (section > 1) |
| 64 | return -ERANGE; |
| 65 | |
| 66 | if (!section) { |
| 67 | oobregion->offset = 0; |
Miquel Raynal | f7f8c17 | 2017-07-05 08:51:09 +0200 | [diff] [blame] | 68 | if (mtd->oobsize == 16) |
| 69 | oobregion->length = 4; |
| 70 | else |
| 71 | oobregion->length = 3; |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 72 | } else { |
Miquel Raynal | f7f8c17 | 2017-07-05 08:51:09 +0200 | [diff] [blame] | 73 | if (mtd->oobsize == 8) |
| 74 | return -ERANGE; |
| 75 | |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 76 | oobregion->offset = 6; |
| 77 | oobregion->length = ecc->total - 4; |
| 78 | } |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section, |
| 84 | struct mtd_oob_region *oobregion) |
| 85 | { |
| 86 | if (section > 1) |
| 87 | return -ERANGE; |
| 88 | |
| 89 | if (mtd->oobsize == 16) { |
| 90 | if (section) |
| 91 | return -ERANGE; |
| 92 | |
| 93 | oobregion->length = 8; |
| 94 | oobregion->offset = 8; |
| 95 | } else { |
| 96 | oobregion->length = 2; |
| 97 | if (!section) |
| 98 | oobregion->offset = 3; |
| 99 | else |
| 100 | oobregion->offset = 6; |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = { |
| 107 | .ecc = nand_ooblayout_ecc_sp, |
| 108 | .free = nand_ooblayout_free_sp, |
| 109 | }; |
| 110 | EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops); |
| 111 | |
| 112 | static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section, |
| 113 | struct mtd_oob_region *oobregion) |
| 114 | { |
| 115 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 116 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 117 | |
Miquel Raynal | 882fd15 | 2017-08-26 17:19:15 +0200 | [diff] [blame] | 118 | if (section || !ecc->total) |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 119 | return -ERANGE; |
| 120 | |
| 121 | oobregion->length = ecc->total; |
| 122 | oobregion->offset = mtd->oobsize - oobregion->length; |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section, |
| 128 | struct mtd_oob_region *oobregion) |
| 129 | { |
| 130 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 131 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 132 | |
| 133 | if (section) |
| 134 | return -ERANGE; |
| 135 | |
| 136 | oobregion->length = mtd->oobsize - ecc->total - 2; |
| 137 | oobregion->offset = 2; |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = { |
| 143 | .ecc = nand_ooblayout_ecc_lp, |
| 144 | .free = nand_ooblayout_free_lp, |
| 145 | }; |
| 146 | EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops); |
Thomas Gleixner | d470a97 | 2006-05-23 23:48:57 +0200 | [diff] [blame] | 147 | |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 148 | /* |
| 149 | * Support the old "large page" layout used for 1-bit Hamming ECC where ECC |
| 150 | * are placed at a fixed offset. |
| 151 | */ |
| 152 | static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section, |
| 153 | struct mtd_oob_region *oobregion) |
| 154 | { |
| 155 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 156 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 157 | |
| 158 | if (section) |
| 159 | return -ERANGE; |
| 160 | |
| 161 | switch (mtd->oobsize) { |
| 162 | case 64: |
| 163 | oobregion->offset = 40; |
| 164 | break; |
| 165 | case 128: |
| 166 | oobregion->offset = 80; |
| 167 | break; |
| 168 | default: |
| 169 | return -EINVAL; |
| 170 | } |
| 171 | |
| 172 | oobregion->length = ecc->total; |
| 173 | if (oobregion->offset + oobregion->length > mtd->oobsize) |
| 174 | return -ERANGE; |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section, |
| 180 | struct mtd_oob_region *oobregion) |
| 181 | { |
| 182 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 183 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 184 | int ecc_offset = 0; |
| 185 | |
| 186 | if (section < 0 || section > 1) |
| 187 | return -ERANGE; |
| 188 | |
| 189 | switch (mtd->oobsize) { |
| 190 | case 64: |
| 191 | ecc_offset = 40; |
| 192 | break; |
| 193 | case 128: |
| 194 | ecc_offset = 80; |
| 195 | break; |
| 196 | default: |
| 197 | return -EINVAL; |
| 198 | } |
| 199 | |
| 200 | if (section == 0) { |
| 201 | oobregion->offset = 2; |
| 202 | oobregion->length = ecc_offset - 2; |
| 203 | } else { |
| 204 | oobregion->offset = ecc_offset + ecc->total; |
| 205 | oobregion->length = mtd->oobsize - oobregion->offset; |
| 206 | } |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
Colin Ian King | d4ed3b9 | 2017-05-04 13:11:00 +0100 | [diff] [blame] | 211 | static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = { |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 212 | .ecc = nand_ooblayout_ecc_lp_hamming, |
| 213 | .free = nand_ooblayout_free_lp_hamming, |
| 214 | }; |
| 215 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 216 | static int check_offs_len(struct mtd_info *mtd, |
| 217 | loff_t ofs, uint64_t len) |
| 218 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 219 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 220 | int ret = 0; |
| 221 | |
| 222 | /* Start address must align on block boundary */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 223 | if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 224 | pr_debug("%s: unaligned address\n", __func__); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 225 | ret = -EINVAL; |
| 226 | } |
| 227 | |
| 228 | /* Length must align on block boundary */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 229 | if (len & ((1ULL << chip->phys_erase_shift) - 1)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 230 | pr_debug("%s: length not block aligned\n", __func__); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 231 | ret = -EINVAL; |
| 232 | } |
| 233 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 234 | return ret; |
| 235 | } |
| 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | /** |
| 238 | * nand_release_device - [GENERIC] release chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 239 | * @mtd: MTD device structure |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 240 | * |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 241 | * Release chip lock and wake up anyone waiting on the device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 243 | static void nand_release_device(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 245 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 247 | /* Release the controller and the chip */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 248 | spin_lock(&chip->controller->lock); |
| 249 | chip->controller->active = NULL; |
| 250 | chip->state = FL_READY; |
| 251 | wake_up(&chip->controller->wq); |
| 252 | spin_unlock(&chip->controller->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /** |
| 256 | * nand_read_byte - [DEFAULT] read one byte from the chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 257 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 259 | * Default read function for 8bit buswidth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 261 | static uint8_t nand_read_byte(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 263 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 264 | return readb(chip->IO_ADDR_R); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /** |
Masanari Iida | 064a769 | 2012-11-09 23:20:58 +0900 | [diff] [blame] | 268 | * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 269 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 271 | * Default read function for 16bit buswidth with endianness conversion. |
| 272 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 274 | static uint8_t nand_read_byte16(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 276 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 277 | return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | * nand_read_word - [DEFAULT] read one word from the chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 282 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 284 | * Default read function for 16bit buswidth without endianness conversion. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | */ |
| 286 | static u16 nand_read_word(struct mtd_info *mtd) |
| 287 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 288 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 289 | return readw(chip->IO_ADDR_R); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | * nand_select_chip - [DEFAULT] control CE line |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 294 | * @mtd: MTD device structure |
| 295 | * @chipnr: chipnumber to select, -1 for deselect |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | * |
| 297 | * Default select function for 1 chip devices. |
| 298 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 299 | static void nand_select_chip(struct mtd_info *mtd, int chipnr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 301 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 302 | |
| 303 | switch (chipnr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | case -1: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 305 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | break; |
| 307 | case 0: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | break; |
| 309 | |
| 310 | default: |
| 311 | BUG(); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /** |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 316 | * nand_write_byte - [DEFAULT] write single byte to chip |
| 317 | * @mtd: MTD device structure |
| 318 | * @byte: value to write |
| 319 | * |
| 320 | * Default function to write a byte to I/O[7:0] |
| 321 | */ |
| 322 | static void nand_write_byte(struct mtd_info *mtd, uint8_t byte) |
| 323 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 324 | struct nand_chip *chip = mtd_to_nand(mtd); |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 325 | |
| 326 | chip->write_buf(mtd, &byte, 1); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16 |
| 331 | * @mtd: MTD device structure |
| 332 | * @byte: value to write |
| 333 | * |
| 334 | * Default function to write a byte to I/O[7:0] on a 16-bit wide chip. |
| 335 | */ |
| 336 | static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte) |
| 337 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 338 | struct nand_chip *chip = mtd_to_nand(mtd); |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 339 | uint16_t word = byte; |
| 340 | |
| 341 | /* |
| 342 | * It's not entirely clear what should happen to I/O[15:8] when writing |
| 343 | * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads: |
| 344 | * |
| 345 | * When the host supports a 16-bit bus width, only data is |
| 346 | * transferred at the 16-bit width. All address and command line |
| 347 | * transfers shall use only the lower 8-bits of the data bus. During |
| 348 | * command transfers, the host may place any value on the upper |
| 349 | * 8-bits of the data bus. During address transfers, the host shall |
| 350 | * set the upper 8-bits of the data bus to 00h. |
| 351 | * |
Miquel Raynal | b958758 | 2018-03-19 14:47:19 +0100 | [diff] [blame] | 352 | * One user of the write_byte callback is nand_set_features. The |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 353 | * four parameters are specified to be written to I/O[7:0], but this is |
| 354 | * neither an address nor a command transfer. Let's assume a 0 on the |
| 355 | * upper I/O lines is OK. |
| 356 | */ |
| 357 | chip->write_buf(mtd, (uint8_t *)&word, 2); |
| 358 | } |
| 359 | |
| 360 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | * nand_write_buf - [DEFAULT] write buffer to chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 362 | * @mtd: MTD device structure |
| 363 | * @buf: data buffer |
| 364 | * @len: number of bytes to write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 366 | * Default write function for 8bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 368 | static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 370 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Alexander Shiyan | 7641383 | 2013-04-13 09:32:13 +0400 | [diff] [blame] | 372 | iowrite8_rep(chip->IO_ADDR_W, buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 376 | * nand_read_buf - [DEFAULT] read chip data into buffer |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 377 | * @mtd: MTD device structure |
| 378 | * @buf: buffer to store date |
| 379 | * @len: number of bytes to read |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 381 | * Default read function for 8bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 383 | static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 385 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
Alexander Shiyan | 7641383 | 2013-04-13 09:32:13 +0400 | [diff] [blame] | 387 | ioread8_rep(chip->IO_ADDR_R, buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | * nand_write_buf16 - [DEFAULT] write buffer to chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 392 | * @mtd: MTD device structure |
| 393 | * @buf: data buffer |
| 394 | * @len: number of bytes to write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 396 | * Default write function for 16bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 398 | static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 400 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | u16 *p = (u16 *) buf; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 402 | |
Alexander Shiyan | 7641383 | 2013-04-13 09:32:13 +0400 | [diff] [blame] | 403 | iowrite16_rep(chip->IO_ADDR_W, p, len >> 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 407 | * nand_read_buf16 - [DEFAULT] read chip data into buffer |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 408 | * @mtd: MTD device structure |
| 409 | * @buf: buffer to store date |
| 410 | * @len: number of bytes to read |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 412 | * Default read function for 16bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 414 | static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 416 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | u16 *p = (u16 *) buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
Alexander Shiyan | 7641383 | 2013-04-13 09:32:13 +0400 | [diff] [blame] | 419 | ioread16_rep(chip->IO_ADDR_R, p, len >> 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 424 | * @mtd: MTD device structure |
| 425 | * @ofs: offset from device start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 427 | * Check, if the block is bad. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | */ |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 429 | static int nand_block_bad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | { |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 431 | int page, page_end, res; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 432 | struct nand_chip *chip = mtd_to_nand(mtd); |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 433 | u8 bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Brian Norris | 5fb1549 | 2011-05-31 16:31:21 -0700 | [diff] [blame] | 435 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
Kevin Cernekee | b60b08b | 2010-05-04 20:58:10 -0700 | [diff] [blame] | 436 | ofs += mtd->erasesize - mtd->writesize; |
| 437 | |
Thomas Knobloch | 1a12f46 | 2007-05-03 07:39:37 +0100 | [diff] [blame] | 438 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 439 | page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1); |
Thomas Knobloch | 1a12f46 | 2007-05-03 07:39:37 +0100 | [diff] [blame] | 440 | |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 441 | for (; page < page_end; page++) { |
| 442 | res = chip->ecc.read_oob(mtd, chip, page); |
| 443 | if (res) |
| 444 | return res; |
| 445 | |
| 446 | bad = chip->oob_poi[chip->badblockpos]; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 447 | |
Brian Norris | cdbec05 | 2012-01-13 18:11:48 -0800 | [diff] [blame] | 448 | if (likely(chip->badblockbits == 8)) |
| 449 | res = bad != 0xFF; |
| 450 | else |
| 451 | res = hweight8(bad) < chip->badblockbits; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 452 | if (res) |
| 453 | return res; |
| 454 | } |
Maxim Levitsky | e0b58d0 | 2010-02-22 20:39:38 +0200 | [diff] [blame] | 455 | |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 456 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /** |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 460 | * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 461 | * @mtd: MTD device structure |
| 462 | * @ofs: offset from device start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 464 | * This is the default implementation, which can be overridden by a hardware |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 465 | * specific driver. It provides the details for writing a bad block marker to a |
| 466 | * block. |
| 467 | */ |
| 468 | static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 469 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 470 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 471 | struct mtd_oob_ops ops; |
| 472 | uint8_t buf[2] = { 0, 0 }; |
| 473 | int ret = 0, res, i = 0; |
| 474 | |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 475 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 476 | ops.oobbuf = buf; |
| 477 | ops.ooboffs = chip->badblockpos; |
| 478 | if (chip->options & NAND_BUSWIDTH_16) { |
| 479 | ops.ooboffs &= ~0x01; |
| 480 | ops.len = ops.ooblen = 2; |
| 481 | } else { |
| 482 | ops.len = ops.ooblen = 1; |
| 483 | } |
| 484 | ops.mode = MTD_OPS_PLACE_OOB; |
| 485 | |
| 486 | /* Write to first/last page(s) if necessary */ |
| 487 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
| 488 | ofs += mtd->erasesize - mtd->writesize; |
| 489 | do { |
| 490 | res = nand_do_write_oob(mtd, ofs, &ops); |
| 491 | if (!ret) |
| 492 | ret = res; |
| 493 | |
| 494 | i++; |
| 495 | ofs += mtd->writesize; |
| 496 | } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2); |
| 497 | |
| 498 | return ret; |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * nand_block_markbad_lowlevel - mark a block bad |
| 503 | * @mtd: MTD device structure |
| 504 | * @ofs: offset from device start |
| 505 | * |
| 506 | * This function performs the generic NAND bad block marking steps (i.e., bad |
| 507 | * block table(s) and/or marker(s)). We only allow the hardware driver to |
| 508 | * specify how to write bad block markers to OOB (chip->block_markbad). |
| 509 | * |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 510 | * We try operations in the following order: |
Mauro Carvalho Chehab | b6f6c29 | 2017-05-13 07:40:36 -0300 | [diff] [blame] | 511 | * |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 512 | * (1) erase the affected block, to allow OOB marker to be written cleanly |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 513 | * (2) write bad block marker to OOB area of affected block (unless flag |
| 514 | * NAND_BBT_NO_OOB_BBM is present) |
| 515 | * (3) update the BBT |
Mauro Carvalho Chehab | b6f6c29 | 2017-05-13 07:40:36 -0300 | [diff] [blame] | 516 | * |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 517 | * Note that we retain the first error encountered in (2) or (3), finish the |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 518 | * procedures, and dump the error in the end. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | */ |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 520 | static int nand_block_markbad_lowlevel(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 522 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 523 | int res, ret = 0; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 524 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 525 | if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) { |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 526 | struct erase_info einfo; |
| 527 | |
| 528 | /* Attempt erase before marking OOB */ |
| 529 | memset(&einfo, 0, sizeof(einfo)); |
| 530 | einfo.mtd = mtd; |
| 531 | einfo.addr = ofs; |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 532 | einfo.len = 1ULL << chip->phys_erase_shift; |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 533 | nand_erase_nand(mtd, &einfo, 0); |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 534 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 535 | /* Write bad block marker to OOB */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 536 | nand_get_device(mtd, FL_WRITING); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 537 | ret = chip->block_markbad(mtd, ofs); |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 538 | nand_release_device(mtd); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 539 | } |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 540 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 541 | /* Mark block bad in BBT */ |
| 542 | if (chip->bbt) { |
| 543 | res = nand_markbad_bbt(mtd, ofs); |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 544 | if (!ret) |
| 545 | ret = res; |
| 546 | } |
| 547 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 548 | if (!ret) |
| 549 | mtd->ecc_stats.badblocks++; |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 550 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 551 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 554 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | * nand_check_wp - [GENERIC] check if the chip is write protected |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 556 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 558 | * Check, if the device is write protected. The function expects, that the |
| 559 | * device is already selected. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 561 | static int nand_check_wp(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 563 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 564 | u8 status; |
| 565 | int ret; |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 566 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 567 | /* Broken xD cards report WP despite being writable */ |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 568 | if (chip->options & NAND_BROKEN_XD) |
| 569 | return 0; |
| 570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | /* Check the WP bit */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 572 | ret = nand_status_op(chip, &status); |
| 573 | if (ret) |
| 574 | return ret; |
| 575 | |
| 576 | return status & NAND_STATUS_WP ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /** |
Gu Zheng | c30e1f7 | 2014-09-03 17:49:10 +0800 | [diff] [blame] | 580 | * nand_block_isreserved - [GENERIC] Check if a block is marked reserved. |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 581 | * @mtd: MTD device structure |
| 582 | * @ofs: offset from device start |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 583 | * |
Gu Zheng | c30e1f7 | 2014-09-03 17:49:10 +0800 | [diff] [blame] | 584 | * Check if the block is marked as reserved. |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 585 | */ |
| 586 | static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
| 587 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 588 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 589 | |
| 590 | if (!chip->bbt) |
| 591 | return 0; |
| 592 | /* Return info from the table */ |
| 593 | return nand_isreserved_bbt(mtd, ofs); |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad |
| 598 | * @mtd: MTD device structure |
| 599 | * @ofs: offset from device start |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 600 | * @allowbbt: 1, if its allowed to access the bbt area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | * |
| 602 | * Check, if the block is bad. Either by reading the bad block table or |
| 603 | * calling of the scan function. |
| 604 | */ |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 605 | static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int allowbbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 607 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 608 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 609 | if (!chip->bbt) |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 610 | return chip->block_bad(mtd, ofs); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | /* Return info from the table */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 613 | return nand_isbad_bbt(mtd, ofs, allowbbt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 616 | /** |
| 617 | * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands. |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 618 | * @mtd: MTD device structure |
| 619 | * @timeo: Timeout |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 620 | * |
| 621 | * Helper function for nand_wait_ready used when needing to wait in interrupt |
| 622 | * context. |
| 623 | */ |
| 624 | static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo) |
| 625 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 626 | struct nand_chip *chip = mtd_to_nand(mtd); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 627 | int i; |
| 628 | |
| 629 | /* Wait for the device to get ready */ |
| 630 | for (i = 0; i < timeo; i++) { |
| 631 | if (chip->dev_ready(mtd)) |
| 632 | break; |
| 633 | touch_softlockup_watchdog(); |
| 634 | mdelay(1); |
| 635 | } |
| 636 | } |
| 637 | |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 638 | /** |
| 639 | * nand_wait_ready - [GENERIC] Wait for the ready pin after commands. |
| 640 | * @mtd: MTD device structure |
| 641 | * |
| 642 | * Wait for the ready pin after a command, and warn if a timeout occurs. |
| 643 | */ |
David Woodhouse | 4b648b0 | 2006-09-25 17:05:24 +0100 | [diff] [blame] | 644 | void nand_wait_ready(struct mtd_info *mtd) |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 645 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 646 | struct nand_chip *chip = mtd_to_nand(mtd); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 647 | unsigned long timeo = 400; |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 648 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 649 | if (in_interrupt() || oops_in_progress) |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 650 | return panic_nand_wait_ready(mtd, timeo); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 651 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 652 | /* Wait until command is processed or timeout occurs */ |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 653 | timeo = jiffies + msecs_to_jiffies(timeo); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 654 | do { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 655 | if (chip->dev_ready(mtd)) |
Ezequiel Garcia | 4c7e054 | 2016-04-12 17:46:41 -0300 | [diff] [blame] | 656 | return; |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 657 | cond_resched(); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 658 | } while (time_before(jiffies, timeo)); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 659 | |
Brian Norris | 9ebfdf5 | 2016-03-04 17:19:23 -0800 | [diff] [blame] | 660 | if (!chip->dev_ready(mtd)) |
| 661 | pr_warn_ratelimited("timeout while waiting for chip to become ready\n"); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 662 | } |
David Woodhouse | 4b648b0 | 2006-09-25 17:05:24 +0100 | [diff] [blame] | 663 | EXPORT_SYMBOL_GPL(nand_wait_ready); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | /** |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 666 | * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands. |
| 667 | * @mtd: MTD device structure |
| 668 | * @timeo: Timeout in ms |
| 669 | * |
| 670 | * Wait for status ready (i.e. command done) or timeout. |
| 671 | */ |
| 672 | static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo) |
| 673 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 674 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 675 | int ret; |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 676 | |
| 677 | timeo = jiffies + msecs_to_jiffies(timeo); |
| 678 | do { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 679 | u8 status; |
| 680 | |
| 681 | ret = nand_read_data_op(chip, &status, sizeof(status), true); |
| 682 | if (ret) |
| 683 | return; |
| 684 | |
| 685 | if (status & NAND_STATUS_READY) |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 686 | break; |
| 687 | touch_softlockup_watchdog(); |
| 688 | } while (time_before(jiffies, timeo)); |
| 689 | }; |
| 690 | |
| 691 | /** |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 692 | * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1 |
| 693 | * @chip: NAND chip structure |
| 694 | * @timeout_ms: Timeout in ms |
| 695 | * |
| 696 | * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1. |
| 697 | * If that does not happen whitin the specified timeout, -ETIMEDOUT is |
| 698 | * returned. |
| 699 | * |
| 700 | * This helper is intended to be used when the controller does not have access |
| 701 | * to the NAND R/B pin. |
| 702 | * |
| 703 | * Be aware that calling this helper from an ->exec_op() implementation means |
| 704 | * ->exec_op() must be re-entrant. |
| 705 | * |
| 706 | * Return 0 if the NAND chip is ready, a negative error otherwise. |
| 707 | */ |
| 708 | int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms) |
| 709 | { |
| 710 | u8 status = 0; |
| 711 | int ret; |
| 712 | |
| 713 | if (!chip->exec_op) |
| 714 | return -ENOTSUPP; |
| 715 | |
| 716 | ret = nand_status_op(chip, NULL); |
| 717 | if (ret) |
| 718 | return ret; |
| 719 | |
| 720 | timeout_ms = jiffies + msecs_to_jiffies(timeout_ms); |
| 721 | do { |
| 722 | ret = nand_read_data_op(chip, &status, sizeof(status), true); |
| 723 | if (ret) |
| 724 | break; |
| 725 | |
| 726 | if (status & NAND_STATUS_READY) |
| 727 | break; |
| 728 | |
| 729 | /* |
| 730 | * Typical lowest execution time for a tR on most NANDs is 10us, |
| 731 | * use this as polling delay before doing something smarter (ie. |
| 732 | * deriving a delay from the timeout value, timeout_ms/ratio). |
| 733 | */ |
| 734 | udelay(10); |
| 735 | } while (time_before(jiffies, timeout_ms)); |
| 736 | |
| 737 | /* |
| 738 | * We have to exit READ_STATUS mode in order to read real data on the |
| 739 | * bus in case the WAITRDY instruction is preceding a DATA_IN |
| 740 | * instruction. |
| 741 | */ |
| 742 | nand_exit_status_op(chip); |
| 743 | |
| 744 | if (ret) |
| 745 | return ret; |
| 746 | |
| 747 | return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT; |
| 748 | }; |
| 749 | EXPORT_SYMBOL_GPL(nand_soft_waitrdy); |
| 750 | |
| 751 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | * nand_command - [DEFAULT] Send command to NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 753 | * @mtd: MTD device structure |
| 754 | * @command: the command to be sent |
| 755 | * @column: the column address for this command, -1 if none |
| 756 | * @page_addr: the page address for this command, -1 if none |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 758 | * Send command to NAND device. This function is used for small page devices |
Artem Bityutskiy | 51148f1 | 2013-03-05 15:00:51 +0200 | [diff] [blame] | 759 | * (512 Bytes per page). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 761 | static void nand_command(struct mtd_info *mtd, unsigned int command, |
| 762 | int column, int page_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 764 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 765 | int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 767 | /* Write out the command to the device */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | if (command == NAND_CMD_SEQIN) { |
| 769 | int readcmd; |
| 770 | |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 771 | if (column >= mtd->writesize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | /* OOB area */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 773 | column -= mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | readcmd = NAND_CMD_READOOB; |
| 775 | } else if (column < 256) { |
| 776 | /* First 256 bytes --> READ0 */ |
| 777 | readcmd = NAND_CMD_READ0; |
| 778 | } else { |
| 779 | column -= 256; |
| 780 | readcmd = NAND_CMD_READ1; |
| 781 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 782 | chip->cmd_ctrl(mtd, readcmd, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 783 | ctrl &= ~NAND_CTRL_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | } |
Miquel Raynal | df46789 | 2017-11-08 17:00:27 +0100 | [diff] [blame] | 785 | if (command != NAND_CMD_NONE) |
| 786 | chip->cmd_ctrl(mtd, command, ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 788 | /* Address cycle, when necessary */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 789 | ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE; |
| 790 | /* Serially input address */ |
| 791 | if (column != -1) { |
| 792 | /* Adjust columns for 16 bit buswidth */ |
Brian Norris | 3dad234 | 2014-01-29 14:08:12 -0800 | [diff] [blame] | 793 | if (chip->options & NAND_BUSWIDTH_16 && |
| 794 | !nand_opcode_8bits(command)) |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 795 | column >>= 1; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 796 | chip->cmd_ctrl(mtd, column, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 797 | ctrl &= ~NAND_CTRL_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | } |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 799 | if (page_addr != -1) { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 800 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 801 | ctrl &= ~NAND_CTRL_CHANGE; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 802 | chip->cmd_ctrl(mtd, page_addr >> 8, ctrl); |
Masahiro Yamada | 14157f8 | 2017-09-13 11:05:50 +0900 | [diff] [blame] | 803 | if (chip->options & NAND_ROW_ADDR_3) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 804 | chip->cmd_ctrl(mtd, page_addr >> 16, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 805 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 806 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 807 | |
| 808 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 809 | * Program and erase have their own busy handlers status and sequential |
| 810 | * in needs no delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 811 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | switch (command) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 813 | |
Miquel Raynal | df46789 | 2017-11-08 17:00:27 +0100 | [diff] [blame] | 814 | case NAND_CMD_NONE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | case NAND_CMD_PAGEPROG: |
| 816 | case NAND_CMD_ERASE1: |
| 817 | case NAND_CMD_ERASE2: |
| 818 | case NAND_CMD_SEQIN: |
| 819 | case NAND_CMD_STATUS: |
Masahiro Yamada | 3158fa0 | 2017-03-23 09:17:49 +0900 | [diff] [blame] | 820 | case NAND_CMD_READID: |
Masahiro Yamada | c5d664a | 2017-03-23 09:17:50 +0900 | [diff] [blame] | 821 | case NAND_CMD_SET_FEATURES: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | return; |
| 823 | |
| 824 | case NAND_CMD_RESET: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 825 | if (chip->dev_ready) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | break; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 827 | udelay(chip->chip_delay); |
| 828 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 829 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 12efdde | 2006-05-24 22:57:09 +0200 | [diff] [blame] | 830 | chip->cmd_ctrl(mtd, |
| 831 | NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 832 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 833 | nand_wait_status_ready(mtd, 250); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | return; |
| 835 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 836 | /* This applies to read commands */ |
Boris Brezillon | 2165c4a | 2017-05-16 18:35:45 +0200 | [diff] [blame] | 837 | case NAND_CMD_READ0: |
| 838 | /* |
| 839 | * READ0 is sometimes used to exit GET STATUS mode. When this |
| 840 | * is the case no address cycles are requested, and we can use |
| 841 | * this information to detect that we should not wait for the |
| 842 | * device to be ready. |
| 843 | */ |
| 844 | if (column == -1 && page_addr == -1) |
| 845 | return; |
| 846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | default: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 848 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | * If we don't have access to the busy pin, we apply the given |
| 850 | * command delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 851 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 852 | if (!chip->dev_ready) { |
| 853 | udelay(chip->chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | return; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 855 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 857 | /* |
| 858 | * Apply this short delay always to ensure that we do wait tWB in |
| 859 | * any case on any machine. |
| 860 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 861 | ndelay(100); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 862 | |
| 863 | nand_wait_ready(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 866 | static void nand_ccs_delay(struct nand_chip *chip) |
| 867 | { |
| 868 | /* |
| 869 | * The controller already takes care of waiting for tCCS when the RNDIN |
| 870 | * or RNDOUT command is sent, return directly. |
| 871 | */ |
| 872 | if (!(chip->options & NAND_WAIT_TCCS)) |
| 873 | return; |
| 874 | |
| 875 | /* |
| 876 | * Wait tCCS_min if it is correctly defined, otherwise wait 500ns |
| 877 | * (which should be safe for all NANDs). |
| 878 | */ |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 879 | if (chip->setup_data_interface) |
| 880 | ndelay(chip->data_interface.timings.sdr.tCCS_min / 1000); |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 881 | else |
| 882 | ndelay(500); |
| 883 | } |
| 884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | /** |
| 886 | * nand_command_lp - [DEFAULT] Send command to NAND large page device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 887 | * @mtd: MTD device structure |
| 888 | * @command: the command to be sent |
| 889 | * @column: the column address for this command, -1 if none |
| 890 | * @page_addr: the page address for this command, -1 if none |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | * |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 892 | * Send command to NAND device. This is the version for the new large page |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 893 | * devices. We don't have the separate regions as we have in the small page |
| 894 | * devices. We must emulate NAND_CMD_READOOB to keep the code compatible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 896 | static void nand_command_lp(struct mtd_info *mtd, unsigned int command, |
| 897 | int column, int page_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 899 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | |
| 901 | /* Emulate NAND_CMD_READOOB */ |
| 902 | if (command == NAND_CMD_READOOB) { |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 903 | column += mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | command = NAND_CMD_READ0; |
| 905 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 906 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 907 | /* Command latch cycle */ |
Miquel Raynal | df46789 | 2017-11-08 17:00:27 +0100 | [diff] [blame] | 908 | if (command != NAND_CMD_NONE) |
| 909 | chip->cmd_ctrl(mtd, command, |
| 910 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | |
| 912 | if (column != -1 || page_addr != -1) { |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 913 | int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | |
| 915 | /* Serially input address */ |
| 916 | if (column != -1) { |
| 917 | /* Adjust columns for 16 bit buswidth */ |
Brian Norris | 3dad234 | 2014-01-29 14:08:12 -0800 | [diff] [blame] | 918 | if (chip->options & NAND_BUSWIDTH_16 && |
| 919 | !nand_opcode_8bits(command)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | column >>= 1; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 921 | chip->cmd_ctrl(mtd, column, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 922 | ctrl &= ~NAND_CTRL_CHANGE; |
Boris Brezillon | fde85cf | 2016-06-15 13:09:51 +0200 | [diff] [blame] | 923 | |
Brian Norris | f5b88de | 2016-10-03 09:49:35 -0700 | [diff] [blame] | 924 | /* Only output a single addr cycle for 8bits opcodes. */ |
Boris Brezillon | fde85cf | 2016-06-15 13:09:51 +0200 | [diff] [blame] | 925 | if (!nand_opcode_8bits(command)) |
| 926 | chip->cmd_ctrl(mtd, column >> 8, ctrl); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 927 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | if (page_addr != -1) { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 929 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
| 930 | chip->cmd_ctrl(mtd, page_addr >> 8, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 931 | NAND_NCE | NAND_ALE); |
Masahiro Yamada | 14157f8 | 2017-09-13 11:05:50 +0900 | [diff] [blame] | 932 | if (chip->options & NAND_ROW_ADDR_3) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 933 | chip->cmd_ctrl(mtd, page_addr >> 16, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 934 | NAND_NCE | NAND_ALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 937 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 938 | |
| 939 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 940 | * Program and erase have their own busy handlers status, sequential |
Gerhard Sittig | 7a442f1 | 2014-03-29 14:36:22 +0100 | [diff] [blame] | 941 | * in and status need no delay. |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 942 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | switch (command) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 944 | |
Miquel Raynal | df46789 | 2017-11-08 17:00:27 +0100 | [diff] [blame] | 945 | case NAND_CMD_NONE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | case NAND_CMD_CACHEDPROG: |
| 947 | case NAND_CMD_PAGEPROG: |
| 948 | case NAND_CMD_ERASE1: |
| 949 | case NAND_CMD_ERASE2: |
| 950 | case NAND_CMD_SEQIN: |
| 951 | case NAND_CMD_STATUS: |
Masahiro Yamada | 3158fa0 | 2017-03-23 09:17:49 +0900 | [diff] [blame] | 952 | case NAND_CMD_READID: |
Masahiro Yamada | c5d664a | 2017-03-23 09:17:50 +0900 | [diff] [blame] | 953 | case NAND_CMD_SET_FEATURES: |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 954 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 956 | case NAND_CMD_RNDIN: |
| 957 | nand_ccs_delay(chip); |
| 958 | return; |
| 959 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | case NAND_CMD_RESET: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 961 | if (chip->dev_ready) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | break; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 963 | udelay(chip->chip_delay); |
Thomas Gleixner | 12efdde | 2006-05-24 22:57:09 +0200 | [diff] [blame] | 964 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
| 965 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 966 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 967 | NAND_NCE | NAND_CTRL_CHANGE); |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 968 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 969 | nand_wait_status_ready(mtd, 250); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | return; |
| 971 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 972 | case NAND_CMD_RNDOUT: |
| 973 | /* No ready / busy check necessary */ |
| 974 | chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, |
| 975 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 976 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 977 | NAND_NCE | NAND_CTRL_CHANGE); |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 978 | |
| 979 | nand_ccs_delay(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 980 | return; |
| 981 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | case NAND_CMD_READ0: |
Boris Brezillon | 2165c4a | 2017-05-16 18:35:45 +0200 | [diff] [blame] | 983 | /* |
| 984 | * READ0 is sometimes used to exit GET STATUS mode. When this |
| 985 | * is the case no address cycles are requested, and we can use |
| 986 | * this information to detect that READSTART should not be |
| 987 | * issued. |
| 988 | */ |
| 989 | if (column == -1 && page_addr == -1) |
| 990 | return; |
| 991 | |
Thomas Gleixner | 12efdde | 2006-05-24 22:57:09 +0200 | [diff] [blame] | 992 | chip->cmd_ctrl(mtd, NAND_CMD_READSTART, |
| 993 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 994 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 995 | NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 996 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 997 | /* This applies to read commands */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | default: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 999 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | * If we don't have access to the busy pin, we apply the given |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1001 | * command delay. |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1002 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1003 | if (!chip->dev_ready) { |
| 1004 | udelay(chip->chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | return; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1006 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | } |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 1008 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1009 | /* |
| 1010 | * Apply this short delay always to ensure that we do wait tWB in |
| 1011 | * any case on any machine. |
| 1012 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1013 | ndelay(100); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 1014 | |
| 1015 | nand_wait_ready(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1019 | * panic_nand_get_device - [GENERIC] Get chip for selected access |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1020 | * @chip: the nand chip descriptor |
| 1021 | * @mtd: MTD device structure |
| 1022 | * @new_state: the state which is requested |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1023 | * |
| 1024 | * Used when in panic, no locks are taken. |
| 1025 | */ |
| 1026 | static void panic_nand_get_device(struct nand_chip *chip, |
| 1027 | struct mtd_info *mtd, int new_state) |
| 1028 | { |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1029 | /* Hardware controller shared among independent devices */ |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1030 | chip->controller->active = chip; |
| 1031 | chip->state = new_state; |
| 1032 | } |
| 1033 | |
| 1034 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | * nand_get_device - [GENERIC] Get chip for selected access |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1036 | * @mtd: MTD device structure |
| 1037 | * @new_state: the state which is requested |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | * |
| 1039 | * Get the device and lock it for exclusive access |
| 1040 | */ |
Thomas Gleixner | 2c0a2be | 2006-05-23 11:50:56 +0200 | [diff] [blame] | 1041 | static int |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 1042 | nand_get_device(struct mtd_info *mtd, int new_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 1044 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1045 | spinlock_t *lock = &chip->controller->lock; |
| 1046 | wait_queue_head_t *wq = &chip->controller->wq; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1047 | DECLARE_WAITQUEUE(wait, current); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1048 | retry: |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 1049 | spin_lock(lock); |
| 1050 | |
vimal singh | b8b3ee9 | 2009-07-09 20:41:22 +0530 | [diff] [blame] | 1051 | /* Hardware controller shared among independent devices */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1052 | if (!chip->controller->active) |
| 1053 | chip->controller->active = chip; |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 1054 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1055 | if (chip->controller->active == chip && chip->state == FL_READY) { |
| 1056 | chip->state = new_state; |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 1057 | spin_unlock(lock); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 1058 | return 0; |
| 1059 | } |
| 1060 | if (new_state == FL_PM_SUSPENDED) { |
Li Yang | 6b0d9a8 | 2009-11-17 14:45:49 -0800 | [diff] [blame] | 1061 | if (chip->controller->active->state == FL_PM_SUSPENDED) { |
| 1062 | chip->state = FL_PM_SUSPENDED; |
| 1063 | spin_unlock(lock); |
| 1064 | return 0; |
Li Yang | 6b0d9a8 | 2009-11-17 14:45:49 -0800 | [diff] [blame] | 1065 | } |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 1066 | } |
| 1067 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1068 | add_wait_queue(wq, &wait); |
| 1069 | spin_unlock(lock); |
| 1070 | schedule(); |
| 1071 | remove_wait_queue(wq, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | goto retry; |
| 1073 | } |
| 1074 | |
| 1075 | /** |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1076 | * panic_nand_wait - [GENERIC] wait until the command is done |
| 1077 | * @mtd: MTD device structure |
| 1078 | * @chip: NAND chip structure |
| 1079 | * @timeo: timeout |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1080 | * |
| 1081 | * Wait for command done. This is a helper function for nand_wait used when |
| 1082 | * 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] | 1083 | * an oops through mtdoops. |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1084 | */ |
| 1085 | static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip, |
| 1086 | unsigned long timeo) |
| 1087 | { |
| 1088 | int i; |
| 1089 | for (i = 0; i < timeo; i++) { |
| 1090 | if (chip->dev_ready) { |
| 1091 | if (chip->dev_ready(mtd)) |
| 1092 | break; |
| 1093 | } else { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1094 | int ret; |
| 1095 | u8 status; |
| 1096 | |
| 1097 | ret = nand_read_data_op(chip, &status, sizeof(status), |
| 1098 | true); |
| 1099 | if (ret) |
| 1100 | return; |
| 1101 | |
| 1102 | if (status & NAND_STATUS_READY) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1103 | break; |
| 1104 | } |
| 1105 | mdelay(1); |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 1106 | } |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | /** |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1110 | * nand_wait - [DEFAULT] wait until the command is done |
| 1111 | * @mtd: MTD device structure |
| 1112 | * @chip: NAND chip structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | * |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1114 | * Wait for command done. This applies to erase and program only. |
Randy Dunlap | 844d3b4 | 2006-06-28 21:48:27 -0700 | [diff] [blame] | 1115 | */ |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 1116 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | { |
| 1118 | |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1119 | unsigned long timeo = 400; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1120 | u8 status; |
| 1121 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1123 | /* |
| 1124 | * Apply this short delay always to ensure that we do wait tWB in any |
| 1125 | * case on any machine. |
| 1126 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1127 | ndelay(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1129 | ret = nand_status_op(chip, NULL); |
| 1130 | if (ret) |
| 1131 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1133 | if (in_interrupt() || oops_in_progress) |
| 1134 | panic_nand_wait(mtd, chip, timeo); |
| 1135 | else { |
Huang Shijie | 6d2559f | 2013-01-30 10:03:56 +0800 | [diff] [blame] | 1136 | timeo = jiffies + msecs_to_jiffies(timeo); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1137 | do { |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1138 | if (chip->dev_ready) { |
| 1139 | if (chip->dev_ready(mtd)) |
| 1140 | break; |
| 1141 | } else { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1142 | ret = nand_read_data_op(chip, &status, |
| 1143 | sizeof(status), true); |
| 1144 | if (ret) |
| 1145 | return ret; |
| 1146 | |
| 1147 | if (status & NAND_STATUS_READY) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1148 | break; |
| 1149 | } |
| 1150 | cond_resched(); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1151 | } while (time_before(jiffies, timeo)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | } |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 1153 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1154 | ret = nand_read_data_op(chip, &status, sizeof(status), true); |
| 1155 | if (ret) |
| 1156 | return ret; |
| 1157 | |
Matthieu CASTET | f251b8d | 2012-11-05 15:00:44 +0100 | [diff] [blame] | 1158 | /* This can happen if in case of timeout or buggy dev_ready */ |
| 1159 | WARN_ON(!(status & NAND_STATUS_READY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | return status; |
| 1161 | } |
| 1162 | |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 1163 | static bool nand_supports_set_get_features(struct nand_chip *chip) |
| 1164 | { |
| 1165 | return (chip->onfi_version && |
| 1166 | (le16_to_cpu(chip->onfi_params.opt_cmd) & |
| 1167 | ONFI_OPT_CMD_SET_GET_FEATURES)); |
| 1168 | } |
| 1169 | |
| 1170 | /** |
| 1171 | * nand_get_features - wrapper to perform a GET_FEATURE |
| 1172 | * @chip: NAND chip info structure |
| 1173 | * @addr: feature address |
| 1174 | * @subfeature_param: the subfeature parameters, a four bytes array |
| 1175 | * |
| 1176 | * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the |
| 1177 | * operation cannot be handled. |
| 1178 | */ |
| 1179 | int nand_get_features(struct nand_chip *chip, int addr, |
| 1180 | u8 *subfeature_param) |
| 1181 | { |
| 1182 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1183 | |
| 1184 | if (!nand_supports_set_get_features(chip)) |
| 1185 | return -ENOTSUPP; |
| 1186 | |
| 1187 | return chip->get_features(mtd, chip, addr, subfeature_param); |
| 1188 | } |
| 1189 | EXPORT_SYMBOL_GPL(nand_get_features); |
| 1190 | |
| 1191 | /** |
| 1192 | * nand_set_features - wrapper to perform a SET_FEATURE |
| 1193 | * @chip: NAND chip info structure |
| 1194 | * @addr: feature address |
| 1195 | * @subfeature_param: the subfeature parameters, a four bytes array |
| 1196 | * |
| 1197 | * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the |
| 1198 | * operation cannot be handled. |
| 1199 | */ |
| 1200 | int nand_set_features(struct nand_chip *chip, int addr, |
| 1201 | u8 *subfeature_param) |
| 1202 | { |
| 1203 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1204 | |
| 1205 | if (!nand_supports_set_get_features(chip)) |
| 1206 | return -ENOTSUPP; |
| 1207 | |
| 1208 | return chip->set_features(mtd, chip, addr, subfeature_param); |
| 1209 | } |
| 1210 | EXPORT_SYMBOL_GPL(nand_set_features); |
| 1211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | /** |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1213 | * nand_reset_data_interface - Reset data interface and timings |
| 1214 | * @chip: The NAND chip |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1215 | * @chipnr: Internal die id |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1216 | * |
| 1217 | * Reset the Data interface and timings to ONFI mode 0. |
| 1218 | * |
| 1219 | * Returns 0 for success or negative error code otherwise. |
| 1220 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1221 | static int nand_reset_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1222 | { |
| 1223 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1224 | int ret; |
| 1225 | |
| 1226 | if (!chip->setup_data_interface) |
| 1227 | return 0; |
| 1228 | |
| 1229 | /* |
| 1230 | * The ONFI specification says: |
| 1231 | * " |
| 1232 | * To transition from NV-DDR or NV-DDR2 to the SDR data |
| 1233 | * interface, the host shall use the Reset (FFh) command |
| 1234 | * using SDR timing mode 0. A device in any timing mode is |
| 1235 | * required to recognize Reset (FFh) command issued in SDR |
| 1236 | * timing mode 0. |
| 1237 | * " |
| 1238 | * |
| 1239 | * Configure the data interface in SDR mode and set the |
| 1240 | * timings to timing mode 0. |
| 1241 | */ |
| 1242 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 1243 | onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0); |
| 1244 | ret = chip->setup_data_interface(mtd, chipnr, &chip->data_interface); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1245 | if (ret) |
| 1246 | pr_err("Failed to configure data interface to SDR timing mode 0\n"); |
| 1247 | |
| 1248 | return ret; |
| 1249 | } |
| 1250 | |
| 1251 | /** |
| 1252 | * nand_setup_data_interface - Setup the best data interface and timings |
| 1253 | * @chip: The NAND chip |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1254 | * @chipnr: Internal die id |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1255 | * |
| 1256 | * Find and configure the best data interface and NAND timings supported by |
| 1257 | * the chip and the driver. |
| 1258 | * First tries to retrieve supported timing modes from ONFI information, |
| 1259 | * and if the NAND chip does not support ONFI, relies on the |
| 1260 | * ->onfi_timing_mode_default specified in the nand_ids table. |
| 1261 | * |
| 1262 | * Returns 0 for success or negative error code otherwise. |
| 1263 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1264 | static int nand_setup_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1265 | { |
| 1266 | struct mtd_info *mtd = nand_to_mtd(chip); |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 1267 | u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = { |
| 1268 | chip->onfi_timing_mode_default, |
| 1269 | }; |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1270 | int ret; |
| 1271 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 1272 | if (!chip->setup_data_interface) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1273 | return 0; |
| 1274 | |
Miquel Raynal | 993447b | 2018-03-19 14:47:21 +0100 | [diff] [blame] | 1275 | /* Change the mode on the chip side (if supported by the NAND chip) */ |
| 1276 | if (nand_supports_set_get_features(chip)) { |
Miquel Raynal | 29714d6 | 2018-03-19 14:47:23 +0100 | [diff] [blame^] | 1277 | chip->select_chip(mtd, chipnr); |
Miquel Raynal | 993447b | 2018-03-19 14:47:21 +0100 | [diff] [blame] | 1278 | ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE, |
| 1279 | tmode_param); |
Miquel Raynal | 29714d6 | 2018-03-19 14:47:23 +0100 | [diff] [blame^] | 1280 | chip->select_chip(mtd, -1); |
Miquel Raynal | 993447b | 2018-03-19 14:47:21 +0100 | [diff] [blame] | 1281 | if (ret) |
| 1282 | return ret; |
| 1283 | } |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1284 | |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 1285 | /* Change the mode on the controller side */ |
| 1286 | return chip->setup_data_interface(mtd, chipnr, &chip->data_interface); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | /** |
| 1290 | * nand_init_data_interface - find the best data interface and timings |
| 1291 | * @chip: The NAND chip |
| 1292 | * |
| 1293 | * Find the best data interface and NAND timings supported by the chip |
| 1294 | * and the driver. |
| 1295 | * First tries to retrieve supported timing modes from ONFI information, |
| 1296 | * and if the NAND chip does not support ONFI, relies on the |
| 1297 | * ->onfi_timing_mode_default specified in the nand_ids table. After this |
| 1298 | * function nand_chip->data_interface is initialized with the best timing mode |
| 1299 | * available. |
| 1300 | * |
| 1301 | * Returns 0 for success or negative error code otherwise. |
| 1302 | */ |
| 1303 | static int nand_init_data_interface(struct nand_chip *chip) |
| 1304 | { |
| 1305 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1306 | int modes, mode, ret; |
| 1307 | |
| 1308 | if (!chip->setup_data_interface) |
| 1309 | return 0; |
| 1310 | |
| 1311 | /* |
| 1312 | * First try to identify the best timings from ONFI parameters and |
| 1313 | * if the NAND does not support ONFI, fallback to the default ONFI |
| 1314 | * timing mode. |
| 1315 | */ |
| 1316 | modes = onfi_get_async_timing_mode(chip); |
| 1317 | if (modes == ONFI_TIMING_MODE_UNKNOWN) { |
| 1318 | if (!chip->onfi_timing_mode_default) |
| 1319 | return 0; |
| 1320 | |
| 1321 | modes = GENMASK(chip->onfi_timing_mode_default, 0); |
| 1322 | } |
| 1323 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1324 | |
| 1325 | for (mode = fls(modes) - 1; mode >= 0; mode--) { |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 1326 | ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1327 | if (ret) |
| 1328 | continue; |
| 1329 | |
Miquel Raynal | d787b8b | 2017-12-22 18:12:41 +0100 | [diff] [blame] | 1330 | /* |
| 1331 | * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the |
| 1332 | * controller supports the requested timings. |
| 1333 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1334 | ret = chip->setup_data_interface(mtd, |
| 1335 | NAND_DATA_IFACE_CHECK_ONLY, |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 1336 | &chip->data_interface); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1337 | if (!ret) { |
| 1338 | chip->onfi_timing_mode_default = mode; |
| 1339 | break; |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1346 | /** |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1347 | * nand_fill_column_cycles - fill the column cycles of an address |
| 1348 | * @chip: The NAND chip |
| 1349 | * @addrs: Array of address cycles to fill |
| 1350 | * @offset_in_page: The offset in the page |
| 1351 | * |
| 1352 | * Fills the first or the first two bytes of the @addrs field depending |
| 1353 | * on the NAND bus width and the page size. |
| 1354 | * |
| 1355 | * Returns the number of cycles needed to encode the column, or a negative |
| 1356 | * error code in case one of the arguments is invalid. |
| 1357 | */ |
| 1358 | static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs, |
| 1359 | unsigned int offset_in_page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1361 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1362 | |
| 1363 | /* Make sure the offset is less than the actual page size. */ |
| 1364 | if (offset_in_page > mtd->writesize + mtd->oobsize) |
| 1365 | return -EINVAL; |
| 1366 | |
| 1367 | /* |
| 1368 | * On small page NANDs, there's a dedicated command to access the OOB |
| 1369 | * area, and the column address is relative to the start of the OOB |
| 1370 | * area, not the start of the page. Asjust the address accordingly. |
| 1371 | */ |
| 1372 | if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize) |
| 1373 | offset_in_page -= mtd->writesize; |
| 1374 | |
| 1375 | /* |
| 1376 | * The offset in page is expressed in bytes, if the NAND bus is 16-bit |
| 1377 | * wide, then it must be divided by 2. |
| 1378 | */ |
| 1379 | if (chip->options & NAND_BUSWIDTH_16) { |
| 1380 | if (WARN_ON(offset_in_page % 2)) |
| 1381 | return -EINVAL; |
| 1382 | |
| 1383 | offset_in_page /= 2; |
| 1384 | } |
| 1385 | |
| 1386 | addrs[0] = offset_in_page; |
| 1387 | |
| 1388 | /* |
| 1389 | * Small page NANDs use 1 cycle for the columns, while large page NANDs |
| 1390 | * need 2 |
| 1391 | */ |
| 1392 | if (mtd->writesize <= 512) |
| 1393 | return 1; |
| 1394 | |
| 1395 | addrs[1] = offset_in_page >> 8; |
| 1396 | |
| 1397 | return 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1400 | static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page, |
| 1401 | unsigned int offset_in_page, void *buf, |
| 1402 | unsigned int len) |
| 1403 | { |
| 1404 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1405 | const struct nand_sdr_timings *sdr = |
| 1406 | nand_get_sdr_timings(&chip->data_interface); |
| 1407 | u8 addrs[4]; |
| 1408 | struct nand_op_instr instrs[] = { |
| 1409 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 1410 | NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1411 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max), |
| 1412 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 1413 | NAND_OP_DATA_IN(len, buf, 0), |
| 1414 | }; |
| 1415 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1416 | int ret; |
| 1417 | |
| 1418 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1419 | if (!len) |
| 1420 | op.ninstrs--; |
| 1421 | |
| 1422 | if (offset_in_page >= mtd->writesize) |
| 1423 | instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB; |
| 1424 | else if (offset_in_page >= 256 && |
| 1425 | !(chip->options & NAND_BUSWIDTH_16)) |
| 1426 | instrs[0].ctx.cmd.opcode = NAND_CMD_READ1; |
| 1427 | |
| 1428 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1429 | if (ret < 0) |
| 1430 | return ret; |
| 1431 | |
| 1432 | addrs[1] = page; |
| 1433 | addrs[2] = page >> 8; |
| 1434 | |
| 1435 | if (chip->options & NAND_ROW_ADDR_3) { |
| 1436 | addrs[3] = page >> 16; |
| 1437 | instrs[1].ctx.addr.naddrs++; |
| 1438 | } |
| 1439 | |
| 1440 | return nand_exec_op(chip, &op); |
| 1441 | } |
| 1442 | |
| 1443 | static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page, |
| 1444 | unsigned int offset_in_page, void *buf, |
| 1445 | unsigned int len) |
| 1446 | { |
| 1447 | const struct nand_sdr_timings *sdr = |
| 1448 | nand_get_sdr_timings(&chip->data_interface); |
| 1449 | u8 addrs[5]; |
| 1450 | struct nand_op_instr instrs[] = { |
| 1451 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 1452 | NAND_OP_ADDR(4, addrs, 0), |
| 1453 | NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1454 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max), |
| 1455 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 1456 | NAND_OP_DATA_IN(len, buf, 0), |
| 1457 | }; |
| 1458 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1459 | int ret; |
| 1460 | |
| 1461 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1462 | if (!len) |
| 1463 | op.ninstrs--; |
| 1464 | |
| 1465 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1466 | if (ret < 0) |
| 1467 | return ret; |
| 1468 | |
| 1469 | addrs[2] = page; |
| 1470 | addrs[3] = page >> 8; |
| 1471 | |
| 1472 | if (chip->options & NAND_ROW_ADDR_3) { |
| 1473 | addrs[4] = page >> 16; |
| 1474 | instrs[1].ctx.addr.naddrs++; |
| 1475 | } |
| 1476 | |
| 1477 | return nand_exec_op(chip, &op); |
| 1478 | } |
| 1479 | |
| 1480 | /** |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1481 | * nand_read_page_op - Do a READ PAGE operation |
| 1482 | * @chip: The NAND chip |
| 1483 | * @page: page to read |
| 1484 | * @offset_in_page: offset within the page |
| 1485 | * @buf: buffer used to store the data |
| 1486 | * @len: length of the buffer |
| 1487 | * |
| 1488 | * This function issues a READ PAGE operation. |
| 1489 | * This function does not select/unselect the CS line. |
| 1490 | * |
| 1491 | * Returns 0 on success, a negative error code otherwise. |
| 1492 | */ |
| 1493 | int nand_read_page_op(struct nand_chip *chip, unsigned int page, |
| 1494 | unsigned int offset_in_page, void *buf, unsigned int len) |
| 1495 | { |
| 1496 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1497 | |
| 1498 | if (len && !buf) |
| 1499 | return -EINVAL; |
| 1500 | |
| 1501 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1502 | return -EINVAL; |
| 1503 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1504 | if (chip->exec_op) { |
| 1505 | if (mtd->writesize > 512) |
| 1506 | return nand_lp_exec_read_page_op(chip, page, |
| 1507 | offset_in_page, buf, |
| 1508 | len); |
| 1509 | |
| 1510 | return nand_sp_exec_read_page_op(chip, page, offset_in_page, |
| 1511 | buf, len); |
| 1512 | } |
| 1513 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1514 | chip->cmdfunc(mtd, NAND_CMD_READ0, offset_in_page, page); |
| 1515 | if (len) |
| 1516 | chip->read_buf(mtd, buf, len); |
| 1517 | |
| 1518 | return 0; |
| 1519 | } |
| 1520 | EXPORT_SYMBOL_GPL(nand_read_page_op); |
| 1521 | |
| 1522 | /** |
| 1523 | * nand_read_param_page_op - Do a READ PARAMETER PAGE operation |
| 1524 | * @chip: The NAND chip |
| 1525 | * @page: parameter page to read |
| 1526 | * @buf: buffer used to store the data |
| 1527 | * @len: length of the buffer |
| 1528 | * |
| 1529 | * This function issues a READ PARAMETER PAGE operation. |
| 1530 | * This function does not select/unselect the CS line. |
| 1531 | * |
| 1532 | * Returns 0 on success, a negative error code otherwise. |
| 1533 | */ |
| 1534 | static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf, |
| 1535 | unsigned int len) |
| 1536 | { |
| 1537 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1538 | unsigned int i; |
| 1539 | u8 *p = buf; |
| 1540 | |
| 1541 | if (len && !buf) |
| 1542 | return -EINVAL; |
| 1543 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1544 | if (chip->exec_op) { |
| 1545 | const struct nand_sdr_timings *sdr = |
| 1546 | nand_get_sdr_timings(&chip->data_interface); |
| 1547 | struct nand_op_instr instrs[] = { |
| 1548 | NAND_OP_CMD(NAND_CMD_PARAM, 0), |
| 1549 | NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1550 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max), |
| 1551 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 1552 | NAND_OP_8BIT_DATA_IN(len, buf, 0), |
| 1553 | }; |
| 1554 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1555 | |
| 1556 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1557 | if (!len) |
| 1558 | op.ninstrs--; |
| 1559 | |
| 1560 | return nand_exec_op(chip, &op); |
| 1561 | } |
| 1562 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1563 | chip->cmdfunc(mtd, NAND_CMD_PARAM, page, -1); |
| 1564 | for (i = 0; i < len; i++) |
| 1565 | p[i] = chip->read_byte(mtd); |
| 1566 | |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
| 1570 | /** |
| 1571 | * nand_change_read_column_op - Do a CHANGE READ COLUMN operation |
| 1572 | * @chip: The NAND chip |
| 1573 | * @offset_in_page: offset within the page |
| 1574 | * @buf: buffer used to store the data |
| 1575 | * @len: length of the buffer |
| 1576 | * @force_8bit: force 8-bit bus access |
| 1577 | * |
| 1578 | * This function issues a CHANGE READ COLUMN operation. |
| 1579 | * This function does not select/unselect the CS line. |
| 1580 | * |
| 1581 | * Returns 0 on success, a negative error code otherwise. |
| 1582 | */ |
| 1583 | int nand_change_read_column_op(struct nand_chip *chip, |
| 1584 | unsigned int offset_in_page, void *buf, |
| 1585 | unsigned int len, bool force_8bit) |
| 1586 | { |
| 1587 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1588 | |
| 1589 | if (len && !buf) |
| 1590 | return -EINVAL; |
| 1591 | |
| 1592 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1593 | return -EINVAL; |
| 1594 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1595 | /* Small page NANDs do not support column change. */ |
| 1596 | if (mtd->writesize <= 512) |
| 1597 | return -ENOTSUPP; |
| 1598 | |
| 1599 | if (chip->exec_op) { |
| 1600 | const struct nand_sdr_timings *sdr = |
| 1601 | nand_get_sdr_timings(&chip->data_interface); |
| 1602 | u8 addrs[2] = {}; |
| 1603 | struct nand_op_instr instrs[] = { |
| 1604 | NAND_OP_CMD(NAND_CMD_RNDOUT, 0), |
| 1605 | NAND_OP_ADDR(2, addrs, 0), |
| 1606 | NAND_OP_CMD(NAND_CMD_RNDOUTSTART, |
| 1607 | PSEC_TO_NSEC(sdr->tCCS_min)), |
| 1608 | NAND_OP_DATA_IN(len, buf, 0), |
| 1609 | }; |
| 1610 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1611 | int ret; |
| 1612 | |
| 1613 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1614 | if (ret < 0) |
| 1615 | return ret; |
| 1616 | |
| 1617 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1618 | if (!len) |
| 1619 | op.ninstrs--; |
| 1620 | |
| 1621 | instrs[3].ctx.data.force_8bit = force_8bit; |
| 1622 | |
| 1623 | return nand_exec_op(chip, &op); |
| 1624 | } |
| 1625 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1626 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset_in_page, -1); |
| 1627 | if (len) |
| 1628 | chip->read_buf(mtd, buf, len); |
| 1629 | |
| 1630 | return 0; |
| 1631 | } |
| 1632 | EXPORT_SYMBOL_GPL(nand_change_read_column_op); |
| 1633 | |
| 1634 | /** |
| 1635 | * nand_read_oob_op - Do a READ OOB operation |
| 1636 | * @chip: The NAND chip |
| 1637 | * @page: page to read |
| 1638 | * @offset_in_oob: offset within the OOB area |
| 1639 | * @buf: buffer used to store the data |
| 1640 | * @len: length of the buffer |
| 1641 | * |
| 1642 | * This function issues a READ OOB operation. |
| 1643 | * This function does not select/unselect the CS line. |
| 1644 | * |
| 1645 | * Returns 0 on success, a negative error code otherwise. |
| 1646 | */ |
| 1647 | int nand_read_oob_op(struct nand_chip *chip, unsigned int page, |
| 1648 | unsigned int offset_in_oob, void *buf, unsigned int len) |
| 1649 | { |
| 1650 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1651 | |
| 1652 | if (len && !buf) |
| 1653 | return -EINVAL; |
| 1654 | |
| 1655 | if (offset_in_oob + len > mtd->oobsize) |
| 1656 | return -EINVAL; |
| 1657 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1658 | if (chip->exec_op) |
| 1659 | return nand_read_page_op(chip, page, |
| 1660 | mtd->writesize + offset_in_oob, |
| 1661 | buf, len); |
| 1662 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1663 | chip->cmdfunc(mtd, NAND_CMD_READOOB, offset_in_oob, page); |
| 1664 | if (len) |
| 1665 | chip->read_buf(mtd, buf, len); |
| 1666 | |
| 1667 | return 0; |
| 1668 | } |
| 1669 | EXPORT_SYMBOL_GPL(nand_read_oob_op); |
| 1670 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1671 | static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page, |
| 1672 | unsigned int offset_in_page, const void *buf, |
| 1673 | unsigned int len, bool prog) |
| 1674 | { |
| 1675 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1676 | const struct nand_sdr_timings *sdr = |
| 1677 | nand_get_sdr_timings(&chip->data_interface); |
| 1678 | u8 addrs[5] = {}; |
| 1679 | struct nand_op_instr instrs[] = { |
| 1680 | /* |
| 1681 | * The first instruction will be dropped if we're dealing |
| 1682 | * with a large page NAND and adjusted if we're dealing |
| 1683 | * with a small page NAND and the page offset is > 255. |
| 1684 | */ |
| 1685 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 1686 | NAND_OP_CMD(NAND_CMD_SEQIN, 0), |
| 1687 | NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)), |
| 1688 | NAND_OP_DATA_OUT(len, buf, 0), |
| 1689 | NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1690 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0), |
| 1691 | }; |
| 1692 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1693 | int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1694 | int ret; |
| 1695 | u8 status; |
| 1696 | |
| 1697 | if (naddrs < 0) |
| 1698 | return naddrs; |
| 1699 | |
| 1700 | addrs[naddrs++] = page; |
| 1701 | addrs[naddrs++] = page >> 8; |
| 1702 | if (chip->options & NAND_ROW_ADDR_3) |
| 1703 | addrs[naddrs++] = page >> 16; |
| 1704 | |
| 1705 | instrs[2].ctx.addr.naddrs = naddrs; |
| 1706 | |
| 1707 | /* Drop the last two instructions if we're not programming the page. */ |
| 1708 | if (!prog) { |
| 1709 | op.ninstrs -= 2; |
| 1710 | /* Also drop the DATA_OUT instruction if empty. */ |
| 1711 | if (!len) |
| 1712 | op.ninstrs--; |
| 1713 | } |
| 1714 | |
| 1715 | if (mtd->writesize <= 512) { |
| 1716 | /* |
| 1717 | * Small pages need some more tweaking: we have to adjust the |
| 1718 | * first instruction depending on the page offset we're trying |
| 1719 | * to access. |
| 1720 | */ |
| 1721 | if (offset_in_page >= mtd->writesize) |
| 1722 | instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB; |
| 1723 | else if (offset_in_page >= 256 && |
| 1724 | !(chip->options & NAND_BUSWIDTH_16)) |
| 1725 | instrs[0].ctx.cmd.opcode = NAND_CMD_READ1; |
| 1726 | } else { |
| 1727 | /* |
| 1728 | * Drop the first command if we're dealing with a large page |
| 1729 | * NAND. |
| 1730 | */ |
| 1731 | op.instrs++; |
| 1732 | op.ninstrs--; |
| 1733 | } |
| 1734 | |
| 1735 | ret = nand_exec_op(chip, &op); |
| 1736 | if (!prog || ret) |
| 1737 | return ret; |
| 1738 | |
| 1739 | ret = nand_status_op(chip, &status); |
| 1740 | if (ret) |
| 1741 | return ret; |
| 1742 | |
| 1743 | return status; |
| 1744 | } |
| 1745 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1746 | /** |
| 1747 | * nand_prog_page_begin_op - starts a PROG PAGE operation |
| 1748 | * @chip: The NAND chip |
| 1749 | * @page: page to write |
| 1750 | * @offset_in_page: offset within the page |
| 1751 | * @buf: buffer containing the data to write to the page |
| 1752 | * @len: length of the buffer |
| 1753 | * |
| 1754 | * This function issues the first half of a PROG PAGE operation. |
| 1755 | * This function does not select/unselect the CS line. |
| 1756 | * |
| 1757 | * Returns 0 on success, a negative error code otherwise. |
| 1758 | */ |
| 1759 | int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page, |
| 1760 | unsigned int offset_in_page, const void *buf, |
| 1761 | unsigned int len) |
| 1762 | { |
| 1763 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1764 | |
| 1765 | if (len && !buf) |
| 1766 | return -EINVAL; |
| 1767 | |
| 1768 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1769 | return -EINVAL; |
| 1770 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1771 | if (chip->exec_op) |
| 1772 | return nand_exec_prog_page_op(chip, page, offset_in_page, buf, |
| 1773 | len, false); |
| 1774 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1775 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page); |
| 1776 | |
| 1777 | if (buf) |
| 1778 | chip->write_buf(mtd, buf, len); |
| 1779 | |
| 1780 | return 0; |
| 1781 | } |
| 1782 | EXPORT_SYMBOL_GPL(nand_prog_page_begin_op); |
| 1783 | |
| 1784 | /** |
| 1785 | * nand_prog_page_end_op - ends a PROG PAGE operation |
| 1786 | * @chip: The NAND chip |
| 1787 | * |
| 1788 | * This function issues the second half of a PROG PAGE operation. |
| 1789 | * This function does not select/unselect the CS line. |
| 1790 | * |
| 1791 | * Returns 0 on success, a negative error code otherwise. |
| 1792 | */ |
| 1793 | int nand_prog_page_end_op(struct nand_chip *chip) |
| 1794 | { |
| 1795 | struct mtd_info *mtd = nand_to_mtd(chip); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1796 | int ret; |
| 1797 | u8 status; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1798 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1799 | if (chip->exec_op) { |
| 1800 | const struct nand_sdr_timings *sdr = |
| 1801 | nand_get_sdr_timings(&chip->data_interface); |
| 1802 | struct nand_op_instr instrs[] = { |
| 1803 | NAND_OP_CMD(NAND_CMD_PAGEPROG, |
| 1804 | PSEC_TO_NSEC(sdr->tWB_max)), |
| 1805 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0), |
| 1806 | }; |
| 1807 | struct nand_operation op = NAND_OPERATION(instrs); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1808 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1809 | ret = nand_exec_op(chip, &op); |
| 1810 | if (ret) |
| 1811 | return ret; |
| 1812 | |
| 1813 | ret = nand_status_op(chip, &status); |
| 1814 | if (ret) |
| 1815 | return ret; |
| 1816 | } else { |
| 1817 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1818 | ret = chip->waitfunc(mtd, chip); |
| 1819 | if (ret < 0) |
| 1820 | return ret; |
| 1821 | |
| 1822 | status = ret; |
| 1823 | } |
| 1824 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1825 | if (status & NAND_STATUS_FAIL) |
| 1826 | return -EIO; |
| 1827 | |
| 1828 | return 0; |
| 1829 | } |
| 1830 | EXPORT_SYMBOL_GPL(nand_prog_page_end_op); |
| 1831 | |
| 1832 | /** |
| 1833 | * nand_prog_page_op - Do a full PROG PAGE operation |
| 1834 | * @chip: The NAND chip |
| 1835 | * @page: page to write |
| 1836 | * @offset_in_page: offset within the page |
| 1837 | * @buf: buffer containing the data to write to the page |
| 1838 | * @len: length of the buffer |
| 1839 | * |
| 1840 | * This function issues a full PROG PAGE operation. |
| 1841 | * This function does not select/unselect the CS line. |
| 1842 | * |
| 1843 | * Returns 0 on success, a negative error code otherwise. |
| 1844 | */ |
| 1845 | int nand_prog_page_op(struct nand_chip *chip, unsigned int page, |
| 1846 | unsigned int offset_in_page, const void *buf, |
| 1847 | unsigned int len) |
| 1848 | { |
| 1849 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1850 | int status; |
| 1851 | |
| 1852 | if (!len || !buf) |
| 1853 | return -EINVAL; |
| 1854 | |
| 1855 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1856 | return -EINVAL; |
| 1857 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1858 | if (chip->exec_op) { |
| 1859 | status = nand_exec_prog_page_op(chip, page, offset_in_page, buf, |
| 1860 | len, true); |
| 1861 | } else { |
| 1862 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, offset_in_page, page); |
| 1863 | chip->write_buf(mtd, buf, len); |
| 1864 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 1865 | status = chip->waitfunc(mtd, chip); |
| 1866 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1867 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1868 | if (status & NAND_STATUS_FAIL) |
| 1869 | return -EIO; |
| 1870 | |
| 1871 | return 0; |
| 1872 | } |
| 1873 | EXPORT_SYMBOL_GPL(nand_prog_page_op); |
| 1874 | |
| 1875 | /** |
| 1876 | * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation |
| 1877 | * @chip: The NAND chip |
| 1878 | * @offset_in_page: offset within the page |
| 1879 | * @buf: buffer containing the data to send to the NAND |
| 1880 | * @len: length of the buffer |
| 1881 | * @force_8bit: force 8-bit bus access |
| 1882 | * |
| 1883 | * This function issues a CHANGE WRITE COLUMN operation. |
| 1884 | * This function does not select/unselect the CS line. |
| 1885 | * |
| 1886 | * Returns 0 on success, a negative error code otherwise. |
| 1887 | */ |
| 1888 | int nand_change_write_column_op(struct nand_chip *chip, |
| 1889 | unsigned int offset_in_page, |
| 1890 | const void *buf, unsigned int len, |
| 1891 | bool force_8bit) |
| 1892 | { |
| 1893 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1894 | |
| 1895 | if (len && !buf) |
| 1896 | return -EINVAL; |
| 1897 | |
| 1898 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1899 | return -EINVAL; |
| 1900 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1901 | /* Small page NANDs do not support column change. */ |
| 1902 | if (mtd->writesize <= 512) |
| 1903 | return -ENOTSUPP; |
| 1904 | |
| 1905 | if (chip->exec_op) { |
| 1906 | const struct nand_sdr_timings *sdr = |
| 1907 | nand_get_sdr_timings(&chip->data_interface); |
| 1908 | u8 addrs[2]; |
| 1909 | struct nand_op_instr instrs[] = { |
| 1910 | NAND_OP_CMD(NAND_CMD_RNDIN, 0), |
| 1911 | NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)), |
| 1912 | NAND_OP_DATA_OUT(len, buf, 0), |
| 1913 | }; |
| 1914 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1915 | int ret; |
| 1916 | |
| 1917 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1918 | if (ret < 0) |
| 1919 | return ret; |
| 1920 | |
| 1921 | instrs[2].ctx.data.force_8bit = force_8bit; |
| 1922 | |
| 1923 | /* Drop the DATA_OUT instruction if len is set to 0. */ |
| 1924 | if (!len) |
| 1925 | op.ninstrs--; |
| 1926 | |
| 1927 | return nand_exec_op(chip, &op); |
| 1928 | } |
| 1929 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1930 | chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset_in_page, -1); |
| 1931 | if (len) |
| 1932 | chip->write_buf(mtd, buf, len); |
| 1933 | |
| 1934 | return 0; |
| 1935 | } |
| 1936 | EXPORT_SYMBOL_GPL(nand_change_write_column_op); |
| 1937 | |
| 1938 | /** |
| 1939 | * nand_readid_op - Do a READID operation |
| 1940 | * @chip: The NAND chip |
| 1941 | * @addr: address cycle to pass after the READID command |
| 1942 | * @buf: buffer used to store the ID |
| 1943 | * @len: length of the buffer |
| 1944 | * |
| 1945 | * This function sends a READID command and reads back the ID returned by the |
| 1946 | * NAND. |
| 1947 | * This function does not select/unselect the CS line. |
| 1948 | * |
| 1949 | * Returns 0 on success, a negative error code otherwise. |
| 1950 | */ |
| 1951 | int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf, |
| 1952 | unsigned int len) |
| 1953 | { |
| 1954 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1955 | unsigned int i; |
| 1956 | u8 *id = buf; |
| 1957 | |
| 1958 | if (len && !buf) |
| 1959 | return -EINVAL; |
| 1960 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1961 | if (chip->exec_op) { |
| 1962 | const struct nand_sdr_timings *sdr = |
| 1963 | nand_get_sdr_timings(&chip->data_interface); |
| 1964 | struct nand_op_instr instrs[] = { |
| 1965 | NAND_OP_CMD(NAND_CMD_READID, 0), |
| 1966 | NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)), |
| 1967 | NAND_OP_8BIT_DATA_IN(len, buf, 0), |
| 1968 | }; |
| 1969 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1970 | |
| 1971 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1972 | if (!len) |
| 1973 | op.ninstrs--; |
| 1974 | |
| 1975 | return nand_exec_op(chip, &op); |
| 1976 | } |
| 1977 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1978 | chip->cmdfunc(mtd, NAND_CMD_READID, addr, -1); |
| 1979 | |
| 1980 | for (i = 0; i < len; i++) |
| 1981 | id[i] = chip->read_byte(mtd); |
| 1982 | |
| 1983 | return 0; |
| 1984 | } |
| 1985 | EXPORT_SYMBOL_GPL(nand_readid_op); |
| 1986 | |
| 1987 | /** |
| 1988 | * nand_status_op - Do a STATUS operation |
| 1989 | * @chip: The NAND chip |
| 1990 | * @status: out variable to store the NAND status |
| 1991 | * |
| 1992 | * This function sends a STATUS command and reads back the status returned by |
| 1993 | * the NAND. |
| 1994 | * This function does not select/unselect the CS line. |
| 1995 | * |
| 1996 | * Returns 0 on success, a negative error code otherwise. |
| 1997 | */ |
| 1998 | int nand_status_op(struct nand_chip *chip, u8 *status) |
| 1999 | { |
| 2000 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 2001 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2002 | if (chip->exec_op) { |
| 2003 | const struct nand_sdr_timings *sdr = |
| 2004 | nand_get_sdr_timings(&chip->data_interface); |
| 2005 | struct nand_op_instr instrs[] = { |
| 2006 | NAND_OP_CMD(NAND_CMD_STATUS, |
| 2007 | PSEC_TO_NSEC(sdr->tADL_min)), |
| 2008 | NAND_OP_8BIT_DATA_IN(1, status, 0), |
| 2009 | }; |
| 2010 | struct nand_operation op = NAND_OPERATION(instrs); |
| 2011 | |
| 2012 | if (!status) |
| 2013 | op.ninstrs--; |
| 2014 | |
| 2015 | return nand_exec_op(chip, &op); |
| 2016 | } |
| 2017 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2018 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
| 2019 | if (status) |
| 2020 | *status = chip->read_byte(mtd); |
| 2021 | |
| 2022 | return 0; |
| 2023 | } |
| 2024 | EXPORT_SYMBOL_GPL(nand_status_op); |
| 2025 | |
| 2026 | /** |
| 2027 | * nand_exit_status_op - Exit a STATUS operation |
| 2028 | * @chip: The NAND chip |
| 2029 | * |
| 2030 | * This function sends a READ0 command to cancel the effect of the STATUS |
| 2031 | * command to avoid reading only the status until a new read command is sent. |
| 2032 | * |
| 2033 | * This function does not select/unselect the CS line. |
| 2034 | * |
| 2035 | * Returns 0 on success, a negative error code otherwise. |
| 2036 | */ |
| 2037 | int nand_exit_status_op(struct nand_chip *chip) |
| 2038 | { |
| 2039 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 2040 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2041 | if (chip->exec_op) { |
| 2042 | struct nand_op_instr instrs[] = { |
| 2043 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 2044 | }; |
| 2045 | struct nand_operation op = NAND_OPERATION(instrs); |
| 2046 | |
| 2047 | return nand_exec_op(chip, &op); |
| 2048 | } |
| 2049 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2050 | chip->cmdfunc(mtd, NAND_CMD_READ0, -1, -1); |
| 2051 | |
| 2052 | return 0; |
| 2053 | } |
| 2054 | EXPORT_SYMBOL_GPL(nand_exit_status_op); |
| 2055 | |
| 2056 | /** |
| 2057 | * nand_erase_op - Do an erase operation |
| 2058 | * @chip: The NAND chip |
| 2059 | * @eraseblock: block to erase |
| 2060 | * |
| 2061 | * This function sends an ERASE command and waits for the NAND to be ready |
| 2062 | * before returning. |
| 2063 | * This function does not select/unselect the CS line. |
| 2064 | * |
| 2065 | * Returns 0 on success, a negative error code otherwise. |
| 2066 | */ |
| 2067 | int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock) |
| 2068 | { |
| 2069 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 2070 | unsigned int page = eraseblock << |
| 2071 | (chip->phys_erase_shift - chip->page_shift); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2072 | int ret; |
| 2073 | u8 status; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2074 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2075 | if (chip->exec_op) { |
| 2076 | const struct nand_sdr_timings *sdr = |
| 2077 | nand_get_sdr_timings(&chip->data_interface); |
| 2078 | u8 addrs[3] = { page, page >> 8, page >> 16 }; |
| 2079 | struct nand_op_instr instrs[] = { |
| 2080 | NAND_OP_CMD(NAND_CMD_ERASE1, 0), |
| 2081 | NAND_OP_ADDR(2, addrs, 0), |
| 2082 | NAND_OP_CMD(NAND_CMD_ERASE2, |
| 2083 | PSEC_TO_MSEC(sdr->tWB_max)), |
| 2084 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0), |
| 2085 | }; |
| 2086 | struct nand_operation op = NAND_OPERATION(instrs); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2087 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2088 | if (chip->options & NAND_ROW_ADDR_3) |
| 2089 | instrs[1].ctx.addr.naddrs++; |
| 2090 | |
| 2091 | ret = nand_exec_op(chip, &op); |
| 2092 | if (ret) |
| 2093 | return ret; |
| 2094 | |
| 2095 | ret = nand_status_op(chip, &status); |
| 2096 | if (ret) |
| 2097 | return ret; |
| 2098 | } else { |
| 2099 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
| 2100 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); |
| 2101 | |
| 2102 | ret = chip->waitfunc(mtd, chip); |
| 2103 | if (ret < 0) |
| 2104 | return ret; |
| 2105 | |
| 2106 | status = ret; |
| 2107 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2108 | |
| 2109 | if (status & NAND_STATUS_FAIL) |
| 2110 | return -EIO; |
| 2111 | |
| 2112 | return 0; |
| 2113 | } |
| 2114 | EXPORT_SYMBOL_GPL(nand_erase_op); |
| 2115 | |
| 2116 | /** |
| 2117 | * nand_set_features_op - Do a SET FEATURES operation |
| 2118 | * @chip: The NAND chip |
| 2119 | * @feature: feature id |
| 2120 | * @data: 4 bytes of data |
| 2121 | * |
| 2122 | * This function sends a SET FEATURES command and waits for the NAND to be |
| 2123 | * ready before returning. |
| 2124 | * This function does not select/unselect the CS line. |
| 2125 | * |
| 2126 | * Returns 0 on success, a negative error code otherwise. |
| 2127 | */ |
| 2128 | static int nand_set_features_op(struct nand_chip *chip, u8 feature, |
| 2129 | const void *data) |
| 2130 | { |
| 2131 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 2132 | const u8 *params = data; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2133 | int i, ret; |
| 2134 | u8 status; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2135 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2136 | if (chip->exec_op) { |
| 2137 | const struct nand_sdr_timings *sdr = |
| 2138 | nand_get_sdr_timings(&chip->data_interface); |
| 2139 | struct nand_op_instr instrs[] = { |
| 2140 | NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0), |
| 2141 | NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)), |
| 2142 | NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data, |
| 2143 | PSEC_TO_NSEC(sdr->tWB_max)), |
| 2144 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0), |
| 2145 | }; |
| 2146 | struct nand_operation op = NAND_OPERATION(instrs); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2147 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2148 | ret = nand_exec_op(chip, &op); |
| 2149 | if (ret) |
| 2150 | return ret; |
| 2151 | |
| 2152 | ret = nand_status_op(chip, &status); |
| 2153 | if (ret) |
| 2154 | return ret; |
| 2155 | } else { |
| 2156 | chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, feature, -1); |
| 2157 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 2158 | chip->write_byte(mtd, params[i]); |
| 2159 | |
| 2160 | ret = chip->waitfunc(mtd, chip); |
| 2161 | if (ret < 0) |
| 2162 | return ret; |
| 2163 | |
| 2164 | status = ret; |
| 2165 | } |
| 2166 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2167 | if (status & NAND_STATUS_FAIL) |
| 2168 | return -EIO; |
| 2169 | |
| 2170 | return 0; |
| 2171 | } |
| 2172 | |
| 2173 | /** |
| 2174 | * nand_get_features_op - Do a GET FEATURES operation |
| 2175 | * @chip: The NAND chip |
| 2176 | * @feature: feature id |
| 2177 | * @data: 4 bytes of data |
| 2178 | * |
| 2179 | * This function sends a GET FEATURES command and waits for the NAND to be |
| 2180 | * ready before returning. |
| 2181 | * This function does not select/unselect the CS line. |
| 2182 | * |
| 2183 | * Returns 0 on success, a negative error code otherwise. |
| 2184 | */ |
| 2185 | static int nand_get_features_op(struct nand_chip *chip, u8 feature, |
| 2186 | void *data) |
| 2187 | { |
| 2188 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 2189 | u8 *params = data; |
| 2190 | int i; |
| 2191 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2192 | if (chip->exec_op) { |
| 2193 | const struct nand_sdr_timings *sdr = |
| 2194 | nand_get_sdr_timings(&chip->data_interface); |
| 2195 | struct nand_op_instr instrs[] = { |
| 2196 | NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0), |
| 2197 | NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)), |
| 2198 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), |
| 2199 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 2200 | NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN, |
| 2201 | data, 0), |
| 2202 | }; |
| 2203 | struct nand_operation op = NAND_OPERATION(instrs); |
| 2204 | |
| 2205 | return nand_exec_op(chip, &op); |
| 2206 | } |
| 2207 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2208 | chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, feature, -1); |
| 2209 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 2210 | params[i] = chip->read_byte(mtd); |
| 2211 | |
| 2212 | return 0; |
| 2213 | } |
| 2214 | |
| 2215 | /** |
| 2216 | * nand_reset_op - Do a reset operation |
| 2217 | * @chip: The NAND chip |
| 2218 | * |
| 2219 | * This function sends a RESET command and waits for the NAND to be ready |
| 2220 | * before returning. |
| 2221 | * This function does not select/unselect the CS line. |
| 2222 | * |
| 2223 | * Returns 0 on success, a negative error code otherwise. |
| 2224 | */ |
| 2225 | int nand_reset_op(struct nand_chip *chip) |
| 2226 | { |
| 2227 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 2228 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2229 | if (chip->exec_op) { |
| 2230 | const struct nand_sdr_timings *sdr = |
| 2231 | nand_get_sdr_timings(&chip->data_interface); |
| 2232 | struct nand_op_instr instrs[] = { |
| 2233 | NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)), |
| 2234 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0), |
| 2235 | }; |
| 2236 | struct nand_operation op = NAND_OPERATION(instrs); |
| 2237 | |
| 2238 | return nand_exec_op(chip, &op); |
| 2239 | } |
| 2240 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2241 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
| 2242 | |
| 2243 | return 0; |
| 2244 | } |
| 2245 | EXPORT_SYMBOL_GPL(nand_reset_op); |
| 2246 | |
| 2247 | /** |
| 2248 | * nand_read_data_op - Read data from the NAND |
| 2249 | * @chip: The NAND chip |
| 2250 | * @buf: buffer used to store the data |
| 2251 | * @len: length of the buffer |
| 2252 | * @force_8bit: force 8-bit bus access |
| 2253 | * |
| 2254 | * This function does a raw data read on the bus. Usually used after launching |
| 2255 | * another NAND operation like nand_read_page_op(). |
| 2256 | * This function does not select/unselect the CS line. |
| 2257 | * |
| 2258 | * Returns 0 on success, a negative error code otherwise. |
| 2259 | */ |
| 2260 | int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len, |
| 2261 | bool force_8bit) |
| 2262 | { |
| 2263 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 2264 | |
| 2265 | if (!len || !buf) |
| 2266 | return -EINVAL; |
| 2267 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2268 | if (chip->exec_op) { |
| 2269 | struct nand_op_instr instrs[] = { |
| 2270 | NAND_OP_DATA_IN(len, buf, 0), |
| 2271 | }; |
| 2272 | struct nand_operation op = NAND_OPERATION(instrs); |
| 2273 | |
| 2274 | instrs[0].ctx.data.force_8bit = force_8bit; |
| 2275 | |
| 2276 | return nand_exec_op(chip, &op); |
| 2277 | } |
| 2278 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2279 | if (force_8bit) { |
| 2280 | u8 *p = buf; |
| 2281 | unsigned int i; |
| 2282 | |
| 2283 | for (i = 0; i < len; i++) |
| 2284 | p[i] = chip->read_byte(mtd); |
| 2285 | } else { |
| 2286 | chip->read_buf(mtd, buf, len); |
| 2287 | } |
| 2288 | |
| 2289 | return 0; |
| 2290 | } |
| 2291 | EXPORT_SYMBOL_GPL(nand_read_data_op); |
| 2292 | |
| 2293 | /** |
| 2294 | * nand_write_data_op - Write data from the NAND |
| 2295 | * @chip: The NAND chip |
| 2296 | * @buf: buffer containing the data to send on the bus |
| 2297 | * @len: length of the buffer |
| 2298 | * @force_8bit: force 8-bit bus access |
| 2299 | * |
| 2300 | * This function does a raw data write on the bus. Usually used after launching |
| 2301 | * another NAND operation like nand_write_page_begin_op(). |
| 2302 | * This function does not select/unselect the CS line. |
| 2303 | * |
| 2304 | * Returns 0 on success, a negative error code otherwise. |
| 2305 | */ |
| 2306 | int nand_write_data_op(struct nand_chip *chip, const void *buf, |
| 2307 | unsigned int len, bool force_8bit) |
| 2308 | { |
| 2309 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 2310 | |
| 2311 | if (!len || !buf) |
| 2312 | return -EINVAL; |
| 2313 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2314 | if (chip->exec_op) { |
| 2315 | struct nand_op_instr instrs[] = { |
| 2316 | NAND_OP_DATA_OUT(len, buf, 0), |
| 2317 | }; |
| 2318 | struct nand_operation op = NAND_OPERATION(instrs); |
| 2319 | |
| 2320 | instrs[0].ctx.data.force_8bit = force_8bit; |
| 2321 | |
| 2322 | return nand_exec_op(chip, &op); |
| 2323 | } |
| 2324 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2325 | if (force_8bit) { |
| 2326 | const u8 *p = buf; |
| 2327 | unsigned int i; |
| 2328 | |
| 2329 | for (i = 0; i < len; i++) |
| 2330 | chip->write_byte(mtd, p[i]); |
| 2331 | } else { |
| 2332 | chip->write_buf(mtd, buf, len); |
| 2333 | } |
| 2334 | |
| 2335 | return 0; |
| 2336 | } |
| 2337 | EXPORT_SYMBOL_GPL(nand_write_data_op); |
| 2338 | |
| 2339 | /** |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2340 | * struct nand_op_parser_ctx - Context used by the parser |
| 2341 | * @instrs: array of all the instructions that must be addressed |
| 2342 | * @ninstrs: length of the @instrs array |
| 2343 | * @subop: Sub-operation to be passed to the NAND controller |
| 2344 | * |
| 2345 | * This structure is used by the core to split NAND operations into |
| 2346 | * sub-operations that can be handled by the NAND controller. |
| 2347 | */ |
| 2348 | struct nand_op_parser_ctx { |
| 2349 | const struct nand_op_instr *instrs; |
| 2350 | unsigned int ninstrs; |
| 2351 | struct nand_subop subop; |
| 2352 | }; |
| 2353 | |
| 2354 | /** |
| 2355 | * nand_op_parser_must_split_instr - Checks if an instruction must be split |
| 2356 | * @pat: the parser pattern element that matches @instr |
| 2357 | * @instr: pointer to the instruction to check |
| 2358 | * @start_offset: this is an in/out parameter. If @instr has already been |
| 2359 | * split, then @start_offset is the offset from which to start |
| 2360 | * (either an address cycle or an offset in the data buffer). |
| 2361 | * Conversely, if the function returns true (ie. instr must be |
| 2362 | * split), this parameter is updated to point to the first |
| 2363 | * data/address cycle that has not been taken care of. |
| 2364 | * |
| 2365 | * Some NAND controllers are limited and cannot send X address cycles with a |
| 2366 | * unique operation, or cannot read/write more than Y bytes at the same time. |
| 2367 | * In this case, split the instruction that does not fit in a single |
| 2368 | * controller-operation into two or more chunks. |
| 2369 | * |
| 2370 | * Returns true if the instruction must be split, false otherwise. |
| 2371 | * The @start_offset parameter is also updated to the offset at which the next |
| 2372 | * bundle of instruction must start (if an address or a data instruction). |
| 2373 | */ |
| 2374 | static bool |
| 2375 | nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat, |
| 2376 | const struct nand_op_instr *instr, |
| 2377 | unsigned int *start_offset) |
| 2378 | { |
| 2379 | switch (pat->type) { |
| 2380 | case NAND_OP_ADDR_INSTR: |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 2381 | if (!pat->ctx.addr.maxcycles) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2382 | break; |
| 2383 | |
| 2384 | if (instr->ctx.addr.naddrs - *start_offset > |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 2385 | pat->ctx.addr.maxcycles) { |
| 2386 | *start_offset += pat->ctx.addr.maxcycles; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2387 | return true; |
| 2388 | } |
| 2389 | break; |
| 2390 | |
| 2391 | case NAND_OP_DATA_IN_INSTR: |
| 2392 | case NAND_OP_DATA_OUT_INSTR: |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 2393 | if (!pat->ctx.data.maxlen) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2394 | break; |
| 2395 | |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 2396 | if (instr->ctx.data.len - *start_offset > |
| 2397 | pat->ctx.data.maxlen) { |
| 2398 | *start_offset += pat->ctx.data.maxlen; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2399 | return true; |
| 2400 | } |
| 2401 | break; |
| 2402 | |
| 2403 | default: |
| 2404 | break; |
| 2405 | } |
| 2406 | |
| 2407 | return false; |
| 2408 | } |
| 2409 | |
| 2410 | /** |
| 2411 | * nand_op_parser_match_pat - Checks if a pattern matches the instructions |
| 2412 | * remaining in the parser context |
| 2413 | * @pat: the pattern to test |
| 2414 | * @ctx: the parser context structure to match with the pattern @pat |
| 2415 | * |
| 2416 | * Check if @pat matches the set or a sub-set of instructions remaining in @ctx. |
| 2417 | * Returns true if this is the case, false ortherwise. When true is returned, |
| 2418 | * @ctx->subop is updated with the set of instructions to be passed to the |
| 2419 | * controller driver. |
| 2420 | */ |
| 2421 | static bool |
| 2422 | nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat, |
| 2423 | struct nand_op_parser_ctx *ctx) |
| 2424 | { |
| 2425 | unsigned int instr_offset = ctx->subop.first_instr_start_off; |
| 2426 | const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs; |
| 2427 | const struct nand_op_instr *instr = ctx->subop.instrs; |
| 2428 | unsigned int i, ninstrs; |
| 2429 | |
| 2430 | for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) { |
| 2431 | /* |
| 2432 | * The pattern instruction does not match the operation |
| 2433 | * instruction. If the instruction is marked optional in the |
| 2434 | * pattern definition, we skip the pattern element and continue |
| 2435 | * to the next one. If the element is mandatory, there's no |
| 2436 | * match and we can return false directly. |
| 2437 | */ |
| 2438 | if (instr->type != pat->elems[i].type) { |
| 2439 | if (!pat->elems[i].optional) |
| 2440 | return false; |
| 2441 | |
| 2442 | continue; |
| 2443 | } |
| 2444 | |
| 2445 | /* |
| 2446 | * Now check the pattern element constraints. If the pattern is |
| 2447 | * not able to handle the whole instruction in a single step, |
| 2448 | * we have to split it. |
| 2449 | * The last_instr_end_off value comes back updated to point to |
| 2450 | * the position where we have to split the instruction (the |
| 2451 | * start of the next subop chunk). |
| 2452 | */ |
| 2453 | if (nand_op_parser_must_split_instr(&pat->elems[i], instr, |
| 2454 | &instr_offset)) { |
| 2455 | ninstrs++; |
| 2456 | i++; |
| 2457 | break; |
| 2458 | } |
| 2459 | |
| 2460 | instr++; |
| 2461 | ninstrs++; |
| 2462 | instr_offset = 0; |
| 2463 | } |
| 2464 | |
| 2465 | /* |
| 2466 | * This can happen if all instructions of a pattern are optional. |
| 2467 | * Still, if there's not at least one instruction handled by this |
| 2468 | * pattern, this is not a match, and we should try the next one (if |
| 2469 | * any). |
| 2470 | */ |
| 2471 | if (!ninstrs) |
| 2472 | return false; |
| 2473 | |
| 2474 | /* |
| 2475 | * We had a match on the pattern head, but the pattern may be longer |
| 2476 | * than the instructions we're asked to execute. We need to make sure |
| 2477 | * there's no mandatory elements in the pattern tail. |
| 2478 | */ |
| 2479 | for (; i < pat->nelems; i++) { |
| 2480 | if (!pat->elems[i].optional) |
| 2481 | return false; |
| 2482 | } |
| 2483 | |
| 2484 | /* |
| 2485 | * We have a match: update the subop structure accordingly and return |
| 2486 | * true. |
| 2487 | */ |
| 2488 | ctx->subop.ninstrs = ninstrs; |
| 2489 | ctx->subop.last_instr_end_off = instr_offset; |
| 2490 | |
| 2491 | return true; |
| 2492 | } |
| 2493 | |
| 2494 | #if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG) |
| 2495 | static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx) |
| 2496 | { |
| 2497 | const struct nand_op_instr *instr; |
| 2498 | char *prefix = " "; |
| 2499 | unsigned int i; |
| 2500 | |
| 2501 | pr_debug("executing subop:\n"); |
| 2502 | |
| 2503 | for (i = 0; i < ctx->ninstrs; i++) { |
| 2504 | instr = &ctx->instrs[i]; |
| 2505 | |
| 2506 | if (instr == &ctx->subop.instrs[0]) |
| 2507 | prefix = " ->"; |
| 2508 | |
| 2509 | switch (instr->type) { |
| 2510 | case NAND_OP_CMD_INSTR: |
| 2511 | pr_debug("%sCMD [0x%02x]\n", prefix, |
| 2512 | instr->ctx.cmd.opcode); |
| 2513 | break; |
| 2514 | case NAND_OP_ADDR_INSTR: |
| 2515 | pr_debug("%sADDR [%d cyc: %*ph]\n", prefix, |
| 2516 | instr->ctx.addr.naddrs, |
| 2517 | instr->ctx.addr.naddrs < 64 ? |
| 2518 | instr->ctx.addr.naddrs : 64, |
| 2519 | instr->ctx.addr.addrs); |
| 2520 | break; |
| 2521 | case NAND_OP_DATA_IN_INSTR: |
| 2522 | pr_debug("%sDATA_IN [%d B%s]\n", prefix, |
| 2523 | instr->ctx.data.len, |
| 2524 | instr->ctx.data.force_8bit ? |
| 2525 | ", force 8-bit" : ""); |
| 2526 | break; |
| 2527 | case NAND_OP_DATA_OUT_INSTR: |
| 2528 | pr_debug("%sDATA_OUT [%d B%s]\n", prefix, |
| 2529 | instr->ctx.data.len, |
| 2530 | instr->ctx.data.force_8bit ? |
| 2531 | ", force 8-bit" : ""); |
| 2532 | break; |
| 2533 | case NAND_OP_WAITRDY_INSTR: |
| 2534 | pr_debug("%sWAITRDY [max %d ms]\n", prefix, |
| 2535 | instr->ctx.waitrdy.timeout_ms); |
| 2536 | break; |
| 2537 | } |
| 2538 | |
| 2539 | if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1]) |
| 2540 | prefix = " "; |
| 2541 | } |
| 2542 | } |
| 2543 | #else |
| 2544 | static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx) |
| 2545 | { |
| 2546 | /* NOP */ |
| 2547 | } |
| 2548 | #endif |
| 2549 | |
| 2550 | /** |
| 2551 | * nand_op_parser_exec_op - exec_op parser |
| 2552 | * @chip: the NAND chip |
| 2553 | * @parser: patterns description provided by the controller driver |
| 2554 | * @op: the NAND operation to address |
| 2555 | * @check_only: when true, the function only checks if @op can be handled but |
| 2556 | * does not execute the operation |
| 2557 | * |
| 2558 | * Helper function designed to ease integration of NAND controller drivers that |
| 2559 | * only support a limited set of instruction sequences. The supported sequences |
| 2560 | * are described in @parser, and the framework takes care of splitting @op into |
| 2561 | * multiple sub-operations (if required) and pass them back to the ->exec() |
| 2562 | * callback of the matching pattern if @check_only is set to false. |
| 2563 | * |
| 2564 | * NAND controller drivers should call this function from their own ->exec_op() |
| 2565 | * implementation. |
| 2566 | * |
| 2567 | * Returns 0 on success, a negative error code otherwise. A failure can be |
| 2568 | * caused by an unsupported operation (none of the supported patterns is able |
| 2569 | * to handle the requested operation), or an error returned by one of the |
| 2570 | * matching pattern->exec() hook. |
| 2571 | */ |
| 2572 | int nand_op_parser_exec_op(struct nand_chip *chip, |
| 2573 | const struct nand_op_parser *parser, |
| 2574 | const struct nand_operation *op, bool check_only) |
| 2575 | { |
| 2576 | struct nand_op_parser_ctx ctx = { |
| 2577 | .subop.instrs = op->instrs, |
| 2578 | .instrs = op->instrs, |
| 2579 | .ninstrs = op->ninstrs, |
| 2580 | }; |
| 2581 | unsigned int i; |
| 2582 | |
| 2583 | while (ctx.subop.instrs < op->instrs + op->ninstrs) { |
| 2584 | int ret; |
| 2585 | |
| 2586 | for (i = 0; i < parser->npatterns; i++) { |
| 2587 | const struct nand_op_parser_pattern *pattern; |
| 2588 | |
| 2589 | pattern = &parser->patterns[i]; |
| 2590 | if (!nand_op_parser_match_pat(pattern, &ctx)) |
| 2591 | continue; |
| 2592 | |
| 2593 | nand_op_parser_trace(&ctx); |
| 2594 | |
| 2595 | if (check_only) |
| 2596 | break; |
| 2597 | |
| 2598 | ret = pattern->exec(chip, &ctx.subop); |
| 2599 | if (ret) |
| 2600 | return ret; |
| 2601 | |
| 2602 | break; |
| 2603 | } |
| 2604 | |
| 2605 | if (i == parser->npatterns) { |
| 2606 | pr_debug("->exec_op() parser: pattern not found!\n"); |
| 2607 | return -ENOTSUPP; |
| 2608 | } |
| 2609 | |
| 2610 | /* |
| 2611 | * Update the context structure by pointing to the start of the |
| 2612 | * next subop. |
| 2613 | */ |
| 2614 | ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs; |
| 2615 | if (ctx.subop.last_instr_end_off) |
| 2616 | ctx.subop.instrs -= 1; |
| 2617 | |
| 2618 | ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off; |
| 2619 | } |
| 2620 | |
| 2621 | return 0; |
| 2622 | } |
| 2623 | EXPORT_SYMBOL_GPL(nand_op_parser_exec_op); |
| 2624 | |
| 2625 | static bool nand_instr_is_data(const struct nand_op_instr *instr) |
| 2626 | { |
| 2627 | return instr && (instr->type == NAND_OP_DATA_IN_INSTR || |
| 2628 | instr->type == NAND_OP_DATA_OUT_INSTR); |
| 2629 | } |
| 2630 | |
| 2631 | static bool nand_subop_instr_is_valid(const struct nand_subop *subop, |
| 2632 | unsigned int instr_idx) |
| 2633 | { |
| 2634 | return subop && instr_idx < subop->ninstrs; |
| 2635 | } |
| 2636 | |
| 2637 | static int nand_subop_get_start_off(const struct nand_subop *subop, |
| 2638 | unsigned int instr_idx) |
| 2639 | { |
| 2640 | if (instr_idx) |
| 2641 | return 0; |
| 2642 | |
| 2643 | return subop->first_instr_start_off; |
| 2644 | } |
| 2645 | |
| 2646 | /** |
| 2647 | * nand_subop_get_addr_start_off - Get the start offset in an address array |
| 2648 | * @subop: The entire sub-operation |
| 2649 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2650 | * |
| 2651 | * During driver development, one could be tempted to directly use the |
| 2652 | * ->addr.addrs field of address instructions. This is wrong as address |
| 2653 | * instructions might be split. |
| 2654 | * |
| 2655 | * Given an address instruction, returns the offset of the first cycle to issue. |
| 2656 | */ |
| 2657 | int nand_subop_get_addr_start_off(const struct nand_subop *subop, |
| 2658 | unsigned int instr_idx) |
| 2659 | { |
| 2660 | if (!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2661 | subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR) |
| 2662 | return -EINVAL; |
| 2663 | |
| 2664 | return nand_subop_get_start_off(subop, instr_idx); |
| 2665 | } |
| 2666 | EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off); |
| 2667 | |
| 2668 | /** |
| 2669 | * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert |
| 2670 | * @subop: The entire sub-operation |
| 2671 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2672 | * |
| 2673 | * During driver development, one could be tempted to directly use the |
| 2674 | * ->addr->naddrs field of a data instruction. This is wrong as instructions |
| 2675 | * might be split. |
| 2676 | * |
| 2677 | * Given an address instruction, returns the number of address cycle to issue. |
| 2678 | */ |
| 2679 | int nand_subop_get_num_addr_cyc(const struct nand_subop *subop, |
| 2680 | unsigned int instr_idx) |
| 2681 | { |
| 2682 | int start_off, end_off; |
| 2683 | |
| 2684 | if (!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2685 | subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR) |
| 2686 | return -EINVAL; |
| 2687 | |
| 2688 | start_off = nand_subop_get_addr_start_off(subop, instr_idx); |
| 2689 | |
| 2690 | if (instr_idx == subop->ninstrs - 1 && |
| 2691 | subop->last_instr_end_off) |
| 2692 | end_off = subop->last_instr_end_off; |
| 2693 | else |
| 2694 | end_off = subop->instrs[instr_idx].ctx.addr.naddrs; |
| 2695 | |
| 2696 | return end_off - start_off; |
| 2697 | } |
| 2698 | EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc); |
| 2699 | |
| 2700 | /** |
| 2701 | * nand_subop_get_data_start_off - Get the start offset in a data array |
| 2702 | * @subop: The entire sub-operation |
| 2703 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2704 | * |
| 2705 | * During driver development, one could be tempted to directly use the |
| 2706 | * ->data->buf.{in,out} field of data instructions. This is wrong as data |
| 2707 | * instructions might be split. |
| 2708 | * |
| 2709 | * Given a data instruction, returns the offset to start from. |
| 2710 | */ |
| 2711 | int nand_subop_get_data_start_off(const struct nand_subop *subop, |
| 2712 | unsigned int instr_idx) |
| 2713 | { |
| 2714 | if (!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2715 | !nand_instr_is_data(&subop->instrs[instr_idx])) |
| 2716 | return -EINVAL; |
| 2717 | |
| 2718 | return nand_subop_get_start_off(subop, instr_idx); |
| 2719 | } |
| 2720 | EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off); |
| 2721 | |
| 2722 | /** |
| 2723 | * nand_subop_get_data_len - Get the number of bytes to retrieve |
| 2724 | * @subop: The entire sub-operation |
| 2725 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2726 | * |
| 2727 | * During driver development, one could be tempted to directly use the |
| 2728 | * ->data->len field of a data instruction. This is wrong as data instructions |
| 2729 | * might be split. |
| 2730 | * |
| 2731 | * Returns the length of the chunk of data to send/receive. |
| 2732 | */ |
| 2733 | int nand_subop_get_data_len(const struct nand_subop *subop, |
| 2734 | unsigned int instr_idx) |
| 2735 | { |
| 2736 | int start_off = 0, end_off; |
| 2737 | |
| 2738 | if (!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2739 | !nand_instr_is_data(&subop->instrs[instr_idx])) |
| 2740 | return -EINVAL; |
| 2741 | |
| 2742 | start_off = nand_subop_get_data_start_off(subop, instr_idx); |
| 2743 | |
| 2744 | if (instr_idx == subop->ninstrs - 1 && |
| 2745 | subop->last_instr_end_off) |
| 2746 | end_off = subop->last_instr_end_off; |
| 2747 | else |
| 2748 | end_off = subop->instrs[instr_idx].ctx.data.len; |
| 2749 | |
| 2750 | return end_off - start_off; |
| 2751 | } |
| 2752 | EXPORT_SYMBOL_GPL(nand_subop_get_data_len); |
| 2753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | /** |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2755 | * nand_reset - Reset and initialize a NAND device |
| 2756 | * @chip: The NAND chip |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2757 | * @chipnr: Internal die id |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2758 | * |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 2759 | * Save the timings data structure, then apply SDR timings mode 0 (see |
| 2760 | * nand_reset_data_interface for details), do the reset operation, and |
| 2761 | * apply back the previous timings. |
| 2762 | * |
| 2763 | * Returns 0 on success, a negative error code otherwise. |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2764 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2765 | int nand_reset(struct nand_chip *chip, int chipnr) |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2766 | { |
| 2767 | struct mtd_info *mtd = nand_to_mtd(chip); |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 2768 | struct nand_data_interface saved_data_intf = chip->data_interface; |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 2769 | int ret; |
| 2770 | |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 2771 | ret = nand_reset_data_interface(chip, chipnr); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 2772 | if (ret) |
| 2773 | return ret; |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2774 | |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2775 | /* |
| 2776 | * The CS line has to be released before we can apply the new NAND |
| 2777 | * interface settings, hence this weird ->select_chip() dance. |
| 2778 | */ |
| 2779 | chip->select_chip(mtd, chipnr); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2780 | ret = nand_reset_op(chip); |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2781 | chip->select_chip(mtd, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2782 | if (ret) |
| 2783 | return ret; |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2784 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 2785 | chip->data_interface = saved_data_intf; |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 2786 | ret = nand_setup_data_interface(chip, chipnr); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 2787 | if (ret) |
| 2788 | return ret; |
| 2789 | |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2790 | return 0; |
| 2791 | } |
Boris Brezillon | b9bb984 | 2017-10-05 18:53:19 +0200 | [diff] [blame] | 2792 | EXPORT_SYMBOL_GPL(nand_reset); |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2793 | |
| 2794 | /** |
Boris BREZILLON | 730a43f | 2015-09-03 18:03:38 +0200 | [diff] [blame] | 2795 | * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data |
| 2796 | * @buf: buffer to test |
| 2797 | * @len: buffer length |
| 2798 | * @bitflips_threshold: maximum number of bitflips |
| 2799 | * |
| 2800 | * Check if a buffer contains only 0xff, which means the underlying region |
| 2801 | * has been erased and is ready to be programmed. |
| 2802 | * The bitflips_threshold specify the maximum number of bitflips before |
| 2803 | * considering the region is not erased. |
| 2804 | * Note: The logic of this function has been extracted from the memweight |
| 2805 | * implementation, except that nand_check_erased_buf function exit before |
| 2806 | * testing the whole buffer if the number of bitflips exceed the |
| 2807 | * bitflips_threshold value. |
| 2808 | * |
| 2809 | * Returns a positive number of bitflips less than or equal to |
| 2810 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 2811 | * threshold. |
| 2812 | */ |
| 2813 | static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold) |
| 2814 | { |
| 2815 | const unsigned char *bitmap = buf; |
| 2816 | int bitflips = 0; |
| 2817 | int weight; |
| 2818 | |
| 2819 | for (; len && ((uintptr_t)bitmap) % sizeof(long); |
| 2820 | len--, bitmap++) { |
| 2821 | weight = hweight8(*bitmap); |
| 2822 | bitflips += BITS_PER_BYTE - weight; |
| 2823 | if (unlikely(bitflips > bitflips_threshold)) |
| 2824 | return -EBADMSG; |
| 2825 | } |
| 2826 | |
| 2827 | for (; len >= sizeof(long); |
| 2828 | len -= sizeof(long), bitmap += sizeof(long)) { |
Pavel Machek | 086567f | 2017-04-21 12:51:07 +0200 | [diff] [blame] | 2829 | unsigned long d = *((unsigned long *)bitmap); |
| 2830 | if (d == ~0UL) |
| 2831 | continue; |
| 2832 | weight = hweight_long(d); |
Boris BREZILLON | 730a43f | 2015-09-03 18:03:38 +0200 | [diff] [blame] | 2833 | bitflips += BITS_PER_LONG - weight; |
| 2834 | if (unlikely(bitflips > bitflips_threshold)) |
| 2835 | return -EBADMSG; |
| 2836 | } |
| 2837 | |
| 2838 | for (; len > 0; len--, bitmap++) { |
| 2839 | weight = hweight8(*bitmap); |
| 2840 | bitflips += BITS_PER_BYTE - weight; |
| 2841 | if (unlikely(bitflips > bitflips_threshold)) |
| 2842 | return -EBADMSG; |
| 2843 | } |
| 2844 | |
| 2845 | return bitflips; |
| 2846 | } |
| 2847 | |
| 2848 | /** |
| 2849 | * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only |
| 2850 | * 0xff data |
| 2851 | * @data: data buffer to test |
| 2852 | * @datalen: data length |
| 2853 | * @ecc: ECC buffer |
| 2854 | * @ecclen: ECC length |
| 2855 | * @extraoob: extra OOB buffer |
| 2856 | * @extraooblen: extra OOB length |
| 2857 | * @bitflips_threshold: maximum number of bitflips |
| 2858 | * |
| 2859 | * Check if a data buffer and its associated ECC and OOB data contains only |
| 2860 | * 0xff pattern, which means the underlying region has been erased and is |
| 2861 | * ready to be programmed. |
| 2862 | * The bitflips_threshold specify the maximum number of bitflips before |
| 2863 | * considering the region as not erased. |
| 2864 | * |
| 2865 | * Note: |
| 2866 | * 1/ ECC algorithms are working on pre-defined block sizes which are usually |
| 2867 | * different from the NAND page size. When fixing bitflips, ECC engines will |
| 2868 | * report the number of errors per chunk, and the NAND core infrastructure |
| 2869 | * expect you to return the maximum number of bitflips for the whole page. |
| 2870 | * This is why you should always use this function on a single chunk and |
| 2871 | * not on the whole page. After checking each chunk you should update your |
| 2872 | * max_bitflips value accordingly. |
| 2873 | * 2/ When checking for bitflips in erased pages you should not only check |
| 2874 | * the payload data but also their associated ECC data, because a user might |
| 2875 | * have programmed almost all bits to 1 but a few. In this case, we |
| 2876 | * shouldn't consider the chunk as erased, and checking ECC bytes prevent |
| 2877 | * this case. |
| 2878 | * 3/ The extraoob argument is optional, and should be used if some of your OOB |
| 2879 | * data are protected by the ECC engine. |
| 2880 | * It could also be used if you support subpages and want to attach some |
| 2881 | * extra OOB data to an ECC chunk. |
| 2882 | * |
| 2883 | * Returns a positive number of bitflips less than or equal to |
| 2884 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 2885 | * threshold. In case of success, the passed buffers are filled with 0xff. |
| 2886 | */ |
| 2887 | int nand_check_erased_ecc_chunk(void *data, int datalen, |
| 2888 | void *ecc, int ecclen, |
| 2889 | void *extraoob, int extraooblen, |
| 2890 | int bitflips_threshold) |
| 2891 | { |
| 2892 | int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0; |
| 2893 | |
| 2894 | data_bitflips = nand_check_erased_buf(data, datalen, |
| 2895 | bitflips_threshold); |
| 2896 | if (data_bitflips < 0) |
| 2897 | return data_bitflips; |
| 2898 | |
| 2899 | bitflips_threshold -= data_bitflips; |
| 2900 | |
| 2901 | ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold); |
| 2902 | if (ecc_bitflips < 0) |
| 2903 | return ecc_bitflips; |
| 2904 | |
| 2905 | bitflips_threshold -= ecc_bitflips; |
| 2906 | |
| 2907 | extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen, |
| 2908 | bitflips_threshold); |
| 2909 | if (extraoob_bitflips < 0) |
| 2910 | return extraoob_bitflips; |
| 2911 | |
| 2912 | if (data_bitflips) |
| 2913 | memset(data, 0xff, datalen); |
| 2914 | |
| 2915 | if (ecc_bitflips) |
| 2916 | memset(ecc, 0xff, ecclen); |
| 2917 | |
| 2918 | if (extraoob_bitflips) |
| 2919 | memset(extraoob, 0xff, extraooblen); |
| 2920 | |
| 2921 | return data_bitflips + ecc_bitflips + extraoob_bitflips; |
| 2922 | } |
| 2923 | EXPORT_SYMBOL(nand_check_erased_ecc_chunk); |
| 2924 | |
| 2925 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2926 | * nand_read_page_raw - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2927 | * @mtd: mtd info structure |
| 2928 | * @chip: nand chip info structure |
| 2929 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2930 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2931 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2932 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2933 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2934 | */ |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 2935 | int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 2936 | uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2937 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2938 | int ret; |
| 2939 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2940 | ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2941 | if (ret) |
| 2942 | return ret; |
| 2943 | |
| 2944 | if (oob_required) { |
| 2945 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 2946 | false); |
| 2947 | if (ret) |
| 2948 | return ret; |
| 2949 | } |
| 2950 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2951 | return 0; |
| 2952 | } |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 2953 | EXPORT_SYMBOL(nand_read_page_raw); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2954 | |
| 2955 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2956 | * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2957 | * @mtd: mtd info structure |
| 2958 | * @chip: nand chip info structure |
| 2959 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2960 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2961 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2962 | * |
| 2963 | * We need a special oob layout and handling even when OOB isn't used. |
| 2964 | */ |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 2965 | static int nand_read_page_raw_syndrome(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2966 | struct nand_chip *chip, uint8_t *buf, |
| 2967 | int oob_required, int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2968 | { |
| 2969 | int eccsize = chip->ecc.size; |
| 2970 | int eccbytes = chip->ecc.bytes; |
| 2971 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2972 | int steps, size, ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2973 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 2974 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 2975 | if (ret) |
| 2976 | return ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2977 | |
| 2978 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2979 | ret = nand_read_data_op(chip, buf, eccsize, false); |
| 2980 | if (ret) |
| 2981 | return ret; |
| 2982 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2983 | buf += eccsize; |
| 2984 | |
| 2985 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2986 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 2987 | false); |
| 2988 | if (ret) |
| 2989 | return ret; |
| 2990 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2991 | oob += chip->ecc.prepad; |
| 2992 | } |
| 2993 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2994 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 2995 | if (ret) |
| 2996 | return ret; |
| 2997 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2998 | oob += eccbytes; |
| 2999 | |
| 3000 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3001 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 3002 | false); |
| 3003 | if (ret) |
| 3004 | return ret; |
| 3005 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3006 | oob += chip->ecc.postpad; |
| 3007 | } |
| 3008 | } |
| 3009 | |
| 3010 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3011 | if (size) { |
| 3012 | ret = nand_read_data_op(chip, oob, size, false); |
| 3013 | if (ret) |
| 3014 | return ret; |
| 3015 | } |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3016 | |
| 3017 | return 0; |
| 3018 | } |
| 3019 | |
| 3020 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3021 | * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3022 | * @mtd: mtd info structure |
| 3023 | * @chip: nand chip info structure |
| 3024 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3025 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3026 | * @page: page number to read |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 3027 | */ |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3028 | static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3029 | uint8_t *buf, int oob_required, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3031 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3032 | int eccbytes = chip->ecc.bytes; |
| 3033 | int eccsteps = chip->ecc.steps; |
| 3034 | uint8_t *p = buf; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3035 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
| 3036 | uint8_t *ecc_code = chip->ecc.code_buf; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3037 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3038 | |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3039 | chip->ecc.read_page_raw(mtd, chip, buf, 1, page); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3040 | |
| 3041 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 3042 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 3043 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3044 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 3045 | chip->ecc.total); |
| 3046 | if (ret) |
| 3047 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3048 | |
| 3049 | eccsteps = chip->ecc.steps; |
| 3050 | p = buf; |
| 3051 | |
| 3052 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 3053 | int stat; |
| 3054 | |
| 3055 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3056 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3057 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3058 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3059 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3060 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 3061 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3062 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3063 | return max_bitflips; |
Thomas Gleixner | 22c60f5 | 2005-04-04 19:56:32 +0100 | [diff] [blame] | 3064 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3065 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | /** |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3067 | * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3068 | * @mtd: mtd info structure |
| 3069 | * @chip: nand chip info structure |
| 3070 | * @data_offs: offset of requested data within the page |
| 3071 | * @readlen: data length |
| 3072 | * @bufpoi: buffer to store read data |
Huang Shijie | e004deb | 2014-01-03 11:01:40 +0800 | [diff] [blame] | 3073 | * @page: page number to read |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3074 | */ |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 3075 | static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, |
Huang Shijie | e004deb | 2014-01-03 11:01:40 +0800 | [diff] [blame] | 3076 | uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi, |
| 3077 | int page) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3078 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3079 | int start_step, end_step, num_steps, ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3080 | uint8_t *p; |
| 3081 | int data_col_addr, i, gaps = 0; |
| 3082 | int datafrag_len, eccfrag_len, aligned_len, aligned_pos; |
| 3083 | int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3084 | int index, section = 0; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3085 | unsigned int max_bitflips = 0; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3086 | struct mtd_oob_region oobregion = { }; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3087 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3088 | /* Column address within the page aligned to ECC size (256bytes) */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3089 | start_step = data_offs / chip->ecc.size; |
| 3090 | end_step = (data_offs + readlen - 1) / chip->ecc.size; |
| 3091 | num_steps = end_step - start_step + 1; |
Ron | 4a4163ca | 2014-03-16 04:01:07 +1030 | [diff] [blame] | 3092 | index = start_step * chip->ecc.bytes; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3093 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3094 | /* Data size aligned to ECC ecc.size */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3095 | datafrag_len = num_steps * chip->ecc.size; |
| 3096 | eccfrag_len = num_steps * chip->ecc.bytes; |
| 3097 | |
| 3098 | data_col_addr = start_step * chip->ecc.size; |
| 3099 | /* If we read not a page aligned data */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3100 | p = bufpoi + data_col_addr; |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3101 | 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] | 3102 | if (ret) |
| 3103 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3104 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3105 | /* Calculate ECC */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3106 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3107 | chip->ecc.calculate(mtd, p, &chip->ecc.calc_buf[i]); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3108 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3109 | /* |
| 3110 | * The performance is faster if we position offsets according to |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3111 | * 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] | 3112 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3113 | ret = mtd_ooblayout_find_eccregion(mtd, index, §ion, &oobregion); |
| 3114 | if (ret) |
| 3115 | return ret; |
| 3116 | |
| 3117 | if (oobregion.length < eccfrag_len) |
| 3118 | gaps = 1; |
| 3119 | |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3120 | if (gaps) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3121 | ret = nand_change_read_column_op(chip, mtd->writesize, |
| 3122 | chip->oob_poi, mtd->oobsize, |
| 3123 | false); |
| 3124 | if (ret) |
| 3125 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3126 | } else { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3127 | /* |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3128 | * Send the command to read the particular ECC bytes take care |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3129 | * about buswidth alignment in read_buf. |
| 3130 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3131 | aligned_pos = oobregion.offset & ~(busw - 1); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3132 | aligned_len = eccfrag_len; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3133 | if (oobregion.offset & (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3134 | aligned_len++; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3135 | if ((oobregion.offset + (num_steps * chip->ecc.bytes)) & |
| 3136 | (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3137 | aligned_len++; |
| 3138 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3139 | ret = nand_change_read_column_op(chip, |
| 3140 | mtd->writesize + aligned_pos, |
| 3141 | &chip->oob_poi[aligned_pos], |
| 3142 | aligned_len, false); |
| 3143 | if (ret) |
| 3144 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3145 | } |
| 3146 | |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3147 | ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf, |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3148 | chip->oob_poi, index, eccfrag_len); |
| 3149 | if (ret) |
| 3150 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3151 | |
| 3152 | p = bufpoi + data_col_addr; |
| 3153 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { |
| 3154 | int stat; |
| 3155 | |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3156 | stat = chip->ecc.correct(mtd, p, &chip->ecc.code_buf[i], |
| 3157 | &chip->ecc.calc_buf[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 3158 | if (stat == -EBADMSG && |
| 3159 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 3160 | /* check for empty pages with bitflips */ |
| 3161 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3162 | &chip->ecc.code_buf[i], |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 3163 | chip->ecc.bytes, |
| 3164 | NULL, 0, |
| 3165 | chip->ecc.strength); |
| 3166 | } |
| 3167 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3168 | if (stat < 0) { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3169 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3170 | } else { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3171 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3172 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 3173 | } |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3174 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3175 | return max_bitflips; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3179 | * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3180 | * @mtd: mtd info structure |
| 3181 | * @chip: nand chip info structure |
| 3182 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3183 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3184 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3185 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3186 | * Not for syndrome calculating ECC controllers which need a special oob layout. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3187 | */ |
| 3188 | static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3189 | uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3190 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3191 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3192 | int eccbytes = chip->ecc.bytes; |
| 3193 | int eccsteps = chip->ecc.steps; |
| 3194 | uint8_t *p = buf; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3195 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
| 3196 | uint8_t *ecc_code = chip->ecc.code_buf; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3197 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3198 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3199 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 3200 | if (ret) |
| 3201 | return ret; |
| 3202 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3203 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 3204 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3205 | |
| 3206 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 3207 | if (ret) |
| 3208 | return ret; |
| 3209 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3210 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 3211 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3212 | |
| 3213 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 3214 | if (ret) |
| 3215 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3216 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3217 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 3218 | chip->ecc.total); |
| 3219 | if (ret) |
| 3220 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3221 | |
| 3222 | eccsteps = chip->ecc.steps; |
| 3223 | p = buf; |
| 3224 | |
| 3225 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 3226 | int stat; |
| 3227 | |
| 3228 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 3229 | if (stat == -EBADMSG && |
| 3230 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 3231 | /* check for empty pages with bitflips */ |
| 3232 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 3233 | &ecc_code[i], eccbytes, |
| 3234 | NULL, 0, |
| 3235 | chip->ecc.strength); |
| 3236 | } |
| 3237 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3238 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3239 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3240 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3241 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3242 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 3243 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3244 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3245 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3246 | } |
| 3247 | |
| 3248 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3249 | * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3250 | * @mtd: mtd info structure |
| 3251 | * @chip: nand chip info structure |
| 3252 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3253 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3254 | * @page: page number to read |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3255 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3256 | * Hardware ECC for large page chips, require OOB to be read first. For this |
| 3257 | * ECC mode, the write_page method is re-used from ECC_HW. These methods |
| 3258 | * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with |
| 3259 | * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from |
| 3260 | * the data area, by overwriting the NAND manufacturer bad block markings. |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3261 | */ |
| 3262 | static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3263 | struct nand_chip *chip, uint8_t *buf, int oob_required, int page) |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3264 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3265 | int i, eccsize = chip->ecc.size, ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3266 | int eccbytes = chip->ecc.bytes; |
| 3267 | int eccsteps = chip->ecc.steps; |
| 3268 | uint8_t *p = buf; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3269 | uint8_t *ecc_code = chip->ecc.code_buf; |
| 3270 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3271 | unsigned int max_bitflips = 0; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3272 | |
| 3273 | /* Read the OOB area first */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3274 | ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); |
| 3275 | if (ret) |
| 3276 | return ret; |
| 3277 | |
| 3278 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 3279 | if (ret) |
| 3280 | return ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3281 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3282 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 3283 | chip->ecc.total); |
| 3284 | if (ret) |
| 3285 | return ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3286 | |
| 3287 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 3288 | int stat; |
| 3289 | |
| 3290 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3291 | |
| 3292 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 3293 | if (ret) |
| 3294 | return ret; |
| 3295 | |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3296 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 3297 | |
| 3298 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 3299 | if (stat == -EBADMSG && |
| 3300 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 3301 | /* check for empty pages with bitflips */ |
| 3302 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 3303 | &ecc_code[i], eccbytes, |
| 3304 | NULL, 0, |
| 3305 | chip->ecc.strength); |
| 3306 | } |
| 3307 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3308 | if (stat < 0) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3309 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3310 | } else { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3311 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3312 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 3313 | } |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3314 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3315 | return max_bitflips; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3316 | } |
| 3317 | |
| 3318 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3319 | * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3320 | * @mtd: mtd info structure |
| 3321 | * @chip: nand chip info structure |
| 3322 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3323 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3324 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3325 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3326 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 3327 | * need a special oob layout and handling. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3328 | */ |
| 3329 | static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3330 | uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3331 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3332 | int ret, i, eccsize = chip->ecc.size; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3333 | int eccbytes = chip->ecc.bytes; |
| 3334 | int eccsteps = chip->ecc.steps; |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 3335 | int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3336 | uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3337 | uint8_t *oob = chip->oob_poi; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3338 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3339 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3340 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 3341 | if (ret) |
| 3342 | return ret; |
| 3343 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3344 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 3345 | int stat; |
| 3346 | |
| 3347 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3348 | |
| 3349 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 3350 | if (ret) |
| 3351 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3352 | |
| 3353 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3354 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 3355 | false); |
| 3356 | if (ret) |
| 3357 | return ret; |
| 3358 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3359 | oob += chip->ecc.prepad; |
| 3360 | } |
| 3361 | |
| 3362 | chip->ecc.hwctl(mtd, NAND_ECC_READSYN); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3363 | |
| 3364 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 3365 | if (ret) |
| 3366 | return ret; |
| 3367 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3368 | stat = chip->ecc.correct(mtd, p, oob, NULL); |
| 3369 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3370 | oob += eccbytes; |
| 3371 | |
| 3372 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3373 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 3374 | false); |
| 3375 | if (ret) |
| 3376 | return ret; |
| 3377 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3378 | oob += chip->ecc.postpad; |
| 3379 | } |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 3380 | |
| 3381 | if (stat == -EBADMSG && |
| 3382 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 3383 | /* check for empty pages with bitflips */ |
| 3384 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 3385 | oob - eccpadbytes, |
| 3386 | eccpadbytes, |
| 3387 | NULL, 0, |
| 3388 | chip->ecc.strength); |
| 3389 | } |
| 3390 | |
| 3391 | if (stat < 0) { |
| 3392 | mtd->ecc_stats.failed++; |
| 3393 | } else { |
| 3394 | mtd->ecc_stats.corrected += stat; |
| 3395 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 3396 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3397 | } |
| 3398 | |
| 3399 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 3400 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3401 | if (i) { |
| 3402 | ret = nand_read_data_op(chip, oob, i, false); |
| 3403 | if (ret) |
| 3404 | return ret; |
| 3405 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3406 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3407 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3408 | } |
| 3409 | |
| 3410 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3411 | * nand_transfer_oob - [INTERN] Transfer oob to client buffer |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3412 | * @mtd: mtd info structure |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3413 | * @oob: oob destination address |
| 3414 | * @ops: oob ops structure |
| 3415 | * @len: size of oob to transfer |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3416 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3417 | static uint8_t *nand_transfer_oob(struct mtd_info *mtd, uint8_t *oob, |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3418 | struct mtd_oob_ops *ops, size_t len) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3419 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3420 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 3421 | int ret; |
| 3422 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3423 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3424 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3425 | case MTD_OPS_PLACE_OOB: |
| 3426 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3427 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); |
| 3428 | return oob + len; |
| 3429 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3430 | case MTD_OPS_AUTO_OOB: |
| 3431 | ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi, |
| 3432 | ops->ooboffs, len); |
| 3433 | BUG_ON(ret); |
| 3434 | return oob + len; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3435 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3436 | default: |
| 3437 | BUG(); |
| 3438 | } |
| 3439 | return NULL; |
| 3440 | } |
| 3441 | |
| 3442 | /** |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3443 | * nand_setup_read_retry - [INTERN] Set the READ RETRY mode |
| 3444 | * @mtd: MTD device structure |
| 3445 | * @retry_mode: the retry mode to use |
| 3446 | * |
| 3447 | * Some vendors supply a special command to shift the Vt threshold, to be used |
| 3448 | * when there are too many bitflips in a page (i.e., ECC error). After setting |
| 3449 | * a new threshold, the host should retry reading the page. |
| 3450 | */ |
| 3451 | static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode) |
| 3452 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3453 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3454 | |
| 3455 | pr_debug("setting READ RETRY mode %d\n", retry_mode); |
| 3456 | |
| 3457 | if (retry_mode >= chip->read_retries) |
| 3458 | return -EINVAL; |
| 3459 | |
| 3460 | if (!chip->setup_read_retry) |
| 3461 | return -EOPNOTSUPP; |
| 3462 | |
| 3463 | return chip->setup_read_retry(mtd, retry_mode); |
| 3464 | } |
| 3465 | |
| 3466 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3467 | * nand_do_read_ops - [INTERN] Read data with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3468 | * @mtd: MTD device structure |
| 3469 | * @from: offset to read from |
| 3470 | * @ops: oob ops structure |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 3471 | * |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3472 | * Internal function. Called with chip held. |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 3473 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3474 | static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, |
| 3475 | struct mtd_oob_ops *ops) |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 3476 | { |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3477 | int chipnr, page, realpage, col, bytes, aligned, oob_required; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3478 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3479 | int ret = 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3480 | uint32_t readlen = ops->len; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3481 | uint32_t oobreadlen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 3482 | uint32_t max_oobsize = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 9aca334 | 2010-02-22 20:39:35 +0200 | [diff] [blame] | 3483 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3484 | uint8_t *bufpoi, *oob, *buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3485 | int use_bufpoi; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3486 | unsigned int max_bitflips = 0; |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3487 | int retry_mode = 0; |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3488 | bool ecc_fail = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3489 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3490 | chipnr = (int)(from >> chip->chip_shift); |
| 3491 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3492 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3493 | realpage = (int)(from >> chip->page_shift); |
| 3494 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3495 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3496 | col = (int)(from & (mtd->writesize - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3497 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3498 | buf = ops->datbuf; |
| 3499 | oob = ops->oobbuf; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3500 | oob_required = oob ? 1 : 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3501 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3502 | while (1) { |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3503 | unsigned int ecc_failures = mtd->ecc_stats.failed; |
| 3504 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3505 | bytes = min(mtd->writesize - col, readlen); |
| 3506 | aligned = (bytes == mtd->writesize); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3507 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3508 | if (!aligned) |
| 3509 | use_bufpoi = 1; |
| 3510 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 3511 | use_bufpoi = !virt_addr_valid(buf) || |
| 3512 | !IS_ALIGNED((unsigned long)buf, |
| 3513 | chip->buf_align); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3514 | else |
| 3515 | use_bufpoi = 0; |
| 3516 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3517 | /* Is the current page in the buffer? */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3518 | if (realpage != chip->pagebuf || oob) { |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3519 | bufpoi = use_bufpoi ? chip->data_buf : buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3520 | |
| 3521 | if (use_bufpoi && aligned) |
| 3522 | pr_debug("%s: using read bounce buffer for buf@%p\n", |
| 3523 | __func__, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3524 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3525 | read_retry: |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3526 | /* |
| 3527 | * Now read the page into the buffer. Absent an error, |
| 3528 | * the read methods return max bitflips per ecc step. |
| 3529 | */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3530 | if (unlikely(ops->mode == MTD_OPS_RAW)) |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3531 | ret = chip->ecc.read_page_raw(mtd, chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3532 | oob_required, |
| 3533 | page); |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 3534 | else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) && |
| 3535 | !oob) |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 3536 | ret = chip->ecc.read_subpage(mtd, chip, |
Huang Shijie | e004deb | 2014-01-03 11:01:40 +0800 | [diff] [blame] | 3537 | col, bytes, bufpoi, |
| 3538 | page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 3539 | else |
Sneha Narnakaje | 46a8cf2 | 2009-09-18 12:51:46 -0700 | [diff] [blame] | 3540 | ret = chip->ecc.read_page(mtd, chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3541 | oob_required, page); |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3542 | if (ret < 0) { |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3543 | if (use_bufpoi) |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3544 | /* Invalidate page cache */ |
| 3545 | chip->pagebuf = -1; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3546 | break; |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3547 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3548 | |
| 3549 | /* Transfer not aligned data */ |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3550 | if (use_bufpoi) { |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 3551 | if (!NAND_HAS_SUBPAGE_READ(chip) && !oob && |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3552 | !(mtd->ecc_stats.failed - ecc_failures) && |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3553 | (ops->mode != MTD_OPS_RAW)) { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3554 | chip->pagebuf = realpage; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3555 | chip->pagebuf_bitflips = ret; |
| 3556 | } else { |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3557 | /* Invalidate page cache */ |
| 3558 | chip->pagebuf = -1; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3559 | } |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3560 | memcpy(buf, chip->data_buf + col, bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3561 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3562 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3563 | if (unlikely(oob)) { |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 3564 | int toread = min(oobreadlen, max_oobsize); |
| 3565 | |
| 3566 | if (toread) { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3567 | oob = nand_transfer_oob(mtd, |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 3568 | oob, ops, toread); |
| 3569 | oobreadlen -= toread; |
| 3570 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3571 | } |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 3572 | |
| 3573 | if (chip->options & NAND_NEED_READRDY) { |
| 3574 | /* Apply delay or wait for ready/busy pin */ |
| 3575 | if (!chip->dev_ready) |
| 3576 | udelay(chip->chip_delay); |
| 3577 | else |
| 3578 | nand_wait_ready(mtd); |
| 3579 | } |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3580 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3581 | if (mtd->ecc_stats.failed - ecc_failures) { |
Brian Norris | 28fa65e | 2014-02-12 16:08:28 -0800 | [diff] [blame] | 3582 | if (retry_mode + 1 < chip->read_retries) { |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3583 | retry_mode++; |
| 3584 | ret = nand_setup_read_retry(mtd, |
| 3585 | retry_mode); |
| 3586 | if (ret < 0) |
| 3587 | break; |
| 3588 | |
| 3589 | /* Reset failures; retry */ |
| 3590 | mtd->ecc_stats.failed = ecc_failures; |
| 3591 | goto read_retry; |
| 3592 | } else { |
| 3593 | /* No more retry modes; real failure */ |
| 3594 | ecc_fail = true; |
| 3595 | } |
| 3596 | } |
| 3597 | |
| 3598 | buf += bytes; |
Masahiro Yamada | 0760468 | 2017-03-30 15:45:47 +0900 | [diff] [blame] | 3599 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3600 | } else { |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3601 | memcpy(buf, chip->data_buf + col, bytes); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3602 | buf += bytes; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3603 | max_bitflips = max_t(unsigned int, max_bitflips, |
| 3604 | chip->pagebuf_bitflips); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3605 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3606 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3607 | readlen -= bytes; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3608 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3609 | /* Reset to retry mode 0 */ |
| 3610 | if (retry_mode) { |
| 3611 | ret = nand_setup_read_retry(mtd, 0); |
| 3612 | if (ret < 0) |
| 3613 | break; |
| 3614 | retry_mode = 0; |
| 3615 | } |
| 3616 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3617 | if (!readlen) |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3618 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3619 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3620 | /* For subsequent reads align to page boundary */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3621 | col = 0; |
| 3622 | /* Increment page address */ |
| 3623 | realpage++; |
| 3624 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3625 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3626 | /* Check, if we cross a chip boundary */ |
| 3627 | if (!page) { |
| 3628 | chipnr++; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3629 | chip->select_chip(mtd, -1); |
| 3630 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3631 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3632 | } |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3633 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3634 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3635 | ops->retlen = ops->len - (size_t) readlen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3636 | if (oob) |
| 3637 | ops->oobretlen = ops->ooblen - oobreadlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3638 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3639 | if (ret < 0) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3640 | return ret; |
| 3641 | |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3642 | if (ecc_fail) |
Thomas Gleixner | 9a1fcdf | 2006-05-29 14:56:39 +0200 | [diff] [blame] | 3643 | return -EBADMSG; |
| 3644 | |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3645 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3646 | } |
| 3647 | |
| 3648 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3649 | * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3650 | * @mtd: mtd info structure |
| 3651 | * @chip: nand chip info structure |
| 3652 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3653 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 3654 | int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3655 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3656 | 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] | 3657 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 3658 | EXPORT_SYMBOL(nand_read_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3659 | |
| 3660 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3661 | * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3662 | * with syndromes |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3663 | * @mtd: mtd info structure |
| 3664 | * @chip: nand chip info structure |
| 3665 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3666 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 3667 | int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
| 3668 | int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3669 | { |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3670 | int length = mtd->oobsize; |
| 3671 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 3672 | int eccsize = chip->ecc.size; |
Baruch Siach | 2ea69d2 | 2015-01-22 15:23:05 +0200 | [diff] [blame] | 3673 | uint8_t *bufpoi = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3674 | int i, toread, sndrnd = 0, pos, ret; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3675 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3676 | ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0); |
| 3677 | if (ret) |
| 3678 | return ret; |
| 3679 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3680 | for (i = 0; i < chip->ecc.steps; i++) { |
| 3681 | if (sndrnd) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3682 | int ret; |
| 3683 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3684 | pos = eccsize + i * (eccsize + chunk); |
| 3685 | if (mtd->writesize > 512) |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3686 | ret = nand_change_read_column_op(chip, pos, |
| 3687 | NULL, 0, |
| 3688 | false); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3689 | else |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3690 | ret = nand_read_page_op(chip, page, pos, NULL, |
| 3691 | 0); |
| 3692 | |
| 3693 | if (ret) |
| 3694 | return ret; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3695 | } else |
| 3696 | sndrnd = 1; |
| 3697 | toread = min_t(int, length, chunk); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3698 | |
| 3699 | ret = nand_read_data_op(chip, bufpoi, toread, false); |
| 3700 | if (ret) |
| 3701 | return ret; |
| 3702 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3703 | bufpoi += toread; |
| 3704 | length -= toread; |
| 3705 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3706 | if (length > 0) { |
| 3707 | ret = nand_read_data_op(chip, bufpoi, length, false); |
| 3708 | if (ret) |
| 3709 | return ret; |
| 3710 | } |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3711 | |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 3712 | return 0; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3713 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 3714 | EXPORT_SYMBOL(nand_read_oob_syndrome); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3715 | |
| 3716 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3717 | * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3718 | * @mtd: mtd info structure |
| 3719 | * @chip: nand chip info structure |
| 3720 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3721 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 3722 | int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3723 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3724 | return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi, |
| 3725 | mtd->oobsize); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3726 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 3727 | EXPORT_SYMBOL(nand_write_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3728 | |
| 3729 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3730 | * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3731 | * with syndrome - only for large page flash |
| 3732 | * @mtd: mtd info structure |
| 3733 | * @chip: nand chip info structure |
| 3734 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3735 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 3736 | int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
| 3737 | int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3738 | { |
| 3739 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 3740 | int eccsize = chip->ecc.size, length = mtd->oobsize; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3741 | int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3742 | const uint8_t *bufpoi = chip->oob_poi; |
| 3743 | |
| 3744 | /* |
| 3745 | * data-ecc-data-ecc ... ecc-oob |
| 3746 | * or |
| 3747 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob |
| 3748 | */ |
| 3749 | if (!chip->ecc.prepad && !chip->ecc.postpad) { |
| 3750 | pos = steps * (eccsize + chunk); |
| 3751 | steps = 0; |
| 3752 | } else |
Vitaly Wool | 8b0036e | 2006-07-11 09:11:25 +0200 | [diff] [blame] | 3753 | pos = eccsize; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3754 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3755 | ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0); |
| 3756 | if (ret) |
| 3757 | return ret; |
| 3758 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3759 | for (i = 0; i < steps; i++) { |
| 3760 | if (sndcmd) { |
| 3761 | if (mtd->writesize <= 512) { |
| 3762 | uint32_t fill = 0xFFFFFFFF; |
| 3763 | |
| 3764 | len = eccsize; |
| 3765 | while (len > 0) { |
| 3766 | int num = min_t(int, len, 4); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3767 | |
| 3768 | ret = nand_write_data_op(chip, &fill, |
| 3769 | num, false); |
| 3770 | if (ret) |
| 3771 | return ret; |
| 3772 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3773 | len -= num; |
| 3774 | } |
| 3775 | } else { |
| 3776 | pos = eccsize + i * (eccsize + chunk); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3777 | ret = nand_change_write_column_op(chip, pos, |
| 3778 | NULL, 0, |
| 3779 | false); |
| 3780 | if (ret) |
| 3781 | return ret; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3782 | } |
| 3783 | } else |
| 3784 | sndcmd = 1; |
| 3785 | len = min_t(int, length, chunk); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3786 | |
| 3787 | ret = nand_write_data_op(chip, bufpoi, len, false); |
| 3788 | if (ret) |
| 3789 | return ret; |
| 3790 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3791 | bufpoi += len; |
| 3792 | length -= len; |
| 3793 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3794 | if (length > 0) { |
| 3795 | ret = nand_write_data_op(chip, bufpoi, length, false); |
| 3796 | if (ret) |
| 3797 | return ret; |
| 3798 | } |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3799 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3800 | return nand_prog_page_end_op(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3801 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 3802 | EXPORT_SYMBOL(nand_write_oob_syndrome); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3803 | |
| 3804 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3805 | * nand_do_read_oob - [INTERN] NAND read out-of-band |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3806 | * @mtd: MTD device structure |
| 3807 | * @from: offset to read from |
| 3808 | * @ops: oob operations description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3809 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3810 | * NAND read out-of-band data from the spare area. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3811 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3812 | static int nand_do_read_oob(struct mtd_info *mtd, loff_t from, |
| 3813 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3814 | { |
Miquel Raynal | 87e89ce | 2018-01-12 10:13:36 +0100 | [diff] [blame] | 3815 | unsigned int max_bitflips = 0; |
Brian Norris | c00a099 | 2012-05-01 17:12:54 -0700 | [diff] [blame] | 3816 | int page, realpage, chipnr; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3817 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 3818 | struct mtd_ecc_stats stats; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3819 | int readlen = ops->ooblen; |
| 3820 | int len; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3821 | uint8_t *buf = ops->oobbuf; |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 3822 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3823 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3824 | pr_debug("%s: from = 0x%08Lx, len = %i\n", |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 3825 | __func__, (unsigned long long)from, readlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3826 | |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 3827 | stats = mtd->ecc_stats; |
| 3828 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 3829 | len = mtd_oobavail(mtd, ops); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 3830 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3831 | chipnr = (int)(from >> chip->chip_shift); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3832 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3833 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3834 | /* Shift to get page */ |
| 3835 | realpage = (int)(from >> chip->page_shift); |
| 3836 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3837 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3838 | while (1) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3839 | if (ops->mode == MTD_OPS_RAW) |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 3840 | ret = chip->ecc.read_oob_raw(mtd, chip, page); |
Brian Norris | c46f648 | 2011-08-30 18:45:38 -0700 | [diff] [blame] | 3841 | else |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 3842 | ret = chip->ecc.read_oob(mtd, chip, page); |
| 3843 | |
| 3844 | if (ret < 0) |
| 3845 | break; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3846 | |
| 3847 | len = min(len, readlen); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3848 | buf = nand_transfer_oob(mtd, buf, ops, len); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3849 | |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 3850 | if (chip->options & NAND_NEED_READRDY) { |
| 3851 | /* Apply delay or wait for ready/busy pin */ |
| 3852 | if (!chip->dev_ready) |
| 3853 | udelay(chip->chip_delay); |
| 3854 | else |
| 3855 | nand_wait_ready(mtd); |
| 3856 | } |
| 3857 | |
Miquel Raynal | 87e89ce | 2018-01-12 10:13:36 +0100 | [diff] [blame] | 3858 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
| 3859 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3860 | readlen -= len; |
Savin Zlobec | 0d420f9 | 2006-06-21 11:51:20 +0200 | [diff] [blame] | 3861 | if (!readlen) |
| 3862 | break; |
| 3863 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3864 | /* Increment page address */ |
| 3865 | realpage++; |
| 3866 | |
| 3867 | page = realpage & chip->pagemask; |
| 3868 | /* Check, if we cross a chip boundary */ |
| 3869 | if (!page) { |
| 3870 | chipnr++; |
| 3871 | chip->select_chip(mtd, -1); |
| 3872 | chip->select_chip(mtd, chipnr); |
| 3873 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3874 | } |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3875 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3876 | |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 3877 | ops->oobretlen = ops->ooblen - readlen; |
| 3878 | |
| 3879 | if (ret < 0) |
| 3880 | return ret; |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 3881 | |
| 3882 | if (mtd->ecc_stats.failed - stats.failed) |
| 3883 | return -EBADMSG; |
| 3884 | |
Miquel Raynal | 87e89ce | 2018-01-12 10:13:36 +0100 | [diff] [blame] | 3885 | return max_bitflips; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3886 | } |
| 3887 | |
| 3888 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3889 | * 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] | 3890 | * @mtd: MTD device structure |
| 3891 | * @from: offset to read from |
| 3892 | * @ops: oob operation description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3893 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3894 | * NAND read data and/or out-of-band data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3895 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3896 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
| 3897 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3898 | { |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 3899 | int ret; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3900 | |
| 3901 | ops->retlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3902 | |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 3903 | if (ops->mode != MTD_OPS_PLACE_OOB && |
| 3904 | ops->mode != MTD_OPS_AUTO_OOB && |
| 3905 | ops->mode != MTD_OPS_RAW) |
| 3906 | return -ENOTSUPP; |
| 3907 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3908 | nand_get_device(mtd, FL_READING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3909 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3910 | if (!ops->datbuf) |
| 3911 | ret = nand_do_read_oob(mtd, from, ops); |
| 3912 | else |
| 3913 | ret = nand_do_read_ops(mtd, from, ops); |
| 3914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3915 | nand_release_device(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3916 | return ret; |
| 3917 | } |
| 3918 | |
| 3919 | |
| 3920 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3921 | * nand_write_page_raw - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3922 | * @mtd: mtd info structure |
| 3923 | * @chip: nand chip info structure |
| 3924 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3925 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3926 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3927 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3928 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3929 | */ |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 3930 | int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 3931 | const uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3932 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3933 | int ret; |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3934 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3935 | ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3936 | if (ret) |
| 3937 | return ret; |
| 3938 | |
| 3939 | if (oob_required) { |
| 3940 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 3941 | false); |
| 3942 | if (ret) |
| 3943 | return ret; |
| 3944 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3945 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3946 | return nand_prog_page_end_op(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3947 | } |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 3948 | EXPORT_SYMBOL(nand_write_page_raw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3949 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3950 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3951 | * nand_write_page_raw_syndrome - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3952 | * @mtd: mtd info structure |
| 3953 | * @chip: nand chip info structure |
| 3954 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3955 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3956 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3957 | * |
| 3958 | * We need a special oob layout and handling even when ECC isn't checked. |
| 3959 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 3960 | static int nand_write_page_raw_syndrome(struct mtd_info *mtd, |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 3961 | struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3962 | const uint8_t *buf, int oob_required, |
| 3963 | int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3964 | { |
| 3965 | int eccsize = chip->ecc.size; |
| 3966 | int eccbytes = chip->ecc.bytes; |
| 3967 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3968 | int steps, size, ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3969 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3970 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 3971 | if (ret) |
| 3972 | return ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3973 | |
| 3974 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3975 | ret = nand_write_data_op(chip, buf, eccsize, false); |
| 3976 | if (ret) |
| 3977 | return ret; |
| 3978 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3979 | buf += eccsize; |
| 3980 | |
| 3981 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3982 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 3983 | false); |
| 3984 | if (ret) |
| 3985 | return ret; |
| 3986 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3987 | oob += chip->ecc.prepad; |
| 3988 | } |
| 3989 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3990 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 3991 | if (ret) |
| 3992 | return ret; |
| 3993 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3994 | oob += eccbytes; |
| 3995 | |
| 3996 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3997 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 3998 | false); |
| 3999 | if (ret) |
| 4000 | return ret; |
| 4001 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 4002 | oob += chip->ecc.postpad; |
| 4003 | } |
| 4004 | } |
| 4005 | |
| 4006 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4007 | if (size) { |
| 4008 | ret = nand_write_data_op(chip, oob, size, false); |
| 4009 | if (ret) |
| 4010 | return ret; |
| 4011 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 4012 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 4013 | return nand_prog_page_end_op(chip); |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 4014 | } |
| 4015 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 4016 | * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4017 | * @mtd: mtd info structure |
| 4018 | * @chip: nand chip info structure |
| 4019 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 4020 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 4021 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4022 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 4023 | static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 4024 | const uint8_t *buf, int oob_required, |
| 4025 | int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4026 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4027 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4028 | int eccbytes = chip->ecc.bytes; |
| 4029 | int eccsteps = chip->ecc.steps; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 4030 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4031 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4032 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 4033 | /* Software ECC calculation */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4034 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 4035 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4036 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4037 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 4038 | chip->ecc.total); |
| 4039 | if (ret) |
| 4040 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4041 | |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 4042 | return chip->ecc.write_page_raw(mtd, chip, buf, 1, page); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4043 | } |
| 4044 | |
| 4045 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 4046 | * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4047 | * @mtd: mtd info structure |
| 4048 | * @chip: nand chip info structure |
| 4049 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 4050 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 4051 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4052 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 4053 | static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 4054 | const uint8_t *buf, int oob_required, |
| 4055 | int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4056 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4057 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4058 | int eccbytes = chip->ecc.bytes; |
| 4059 | int eccsteps = chip->ecc.steps; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 4060 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4061 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4062 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 4063 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 4064 | if (ret) |
| 4065 | return ret; |
| 4066 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4067 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 4068 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4069 | |
| 4070 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 4071 | if (ret) |
| 4072 | return ret; |
| 4073 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4074 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 4075 | } |
| 4076 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4077 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 4078 | chip->ecc.total); |
| 4079 | if (ret) |
| 4080 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4081 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4082 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 4083 | if (ret) |
| 4084 | return ret; |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 4085 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 4086 | return nand_prog_page_end_op(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4087 | } |
| 4088 | |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4089 | |
| 4090 | /** |
Brian Norris | 73c8aaf | 2015-02-28 02:04:18 -0800 | [diff] [blame] | 4091 | * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4092 | * @mtd: mtd info structure |
| 4093 | * @chip: nand chip info structure |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 4094 | * @offset: column address of subpage within the page |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4095 | * @data_len: data length |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 4096 | * @buf: data buffer |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4097 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 4098 | * @page: page number to write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4099 | */ |
| 4100 | static int nand_write_subpage_hwecc(struct mtd_info *mtd, |
| 4101 | struct nand_chip *chip, uint32_t offset, |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 4102 | uint32_t data_len, const uint8_t *buf, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 4103 | int oob_required, int page) |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4104 | { |
| 4105 | uint8_t *oob_buf = chip->oob_poi; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 4106 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4107 | int ecc_size = chip->ecc.size; |
| 4108 | int ecc_bytes = chip->ecc.bytes; |
| 4109 | int ecc_steps = chip->ecc.steps; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4110 | uint32_t start_step = offset / ecc_size; |
| 4111 | uint32_t end_step = (offset + data_len - 1) / ecc_size; |
| 4112 | int oob_bytes = mtd->oobsize / ecc_steps; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4113 | int step, ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4114 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 4115 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 4116 | if (ret) |
| 4117 | return ret; |
| 4118 | |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4119 | for (step = 0; step < ecc_steps; step++) { |
| 4120 | /* configure controller for WRITE access */ |
| 4121 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 4122 | |
| 4123 | /* write data (untouched subpages already masked by 0xFF) */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4124 | ret = nand_write_data_op(chip, buf, ecc_size, false); |
| 4125 | if (ret) |
| 4126 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4127 | |
| 4128 | /* mask ECC of un-touched subpages by padding 0xFF */ |
| 4129 | if ((step < start_step) || (step > end_step)) |
| 4130 | memset(ecc_calc, 0xff, ecc_bytes); |
| 4131 | else |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 4132 | chip->ecc.calculate(mtd, buf, ecc_calc); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4133 | |
| 4134 | /* mask OOB of un-touched subpages by padding 0xFF */ |
| 4135 | /* if oob_required, preserve OOB metadata of written subpage */ |
| 4136 | if (!oob_required || (step < start_step) || (step > end_step)) |
| 4137 | memset(oob_buf, 0xff, oob_bytes); |
| 4138 | |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 4139 | buf += ecc_size; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4140 | ecc_calc += ecc_bytes; |
| 4141 | oob_buf += oob_bytes; |
| 4142 | } |
| 4143 | |
| 4144 | /* copy calculated ECC for whole page to chip->buffer->oob */ |
| 4145 | /* this include masked-value(0xFF) for unwritten subpages */ |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 4146 | ecc_calc = chip->ecc.calc_buf; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4147 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 4148 | chip->ecc.total); |
| 4149 | if (ret) |
| 4150 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4151 | |
| 4152 | /* write OOB buffer to NAND device */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4153 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 4154 | if (ret) |
| 4155 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4156 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 4157 | return nand_prog_page_end_op(chip); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4158 | } |
| 4159 | |
| 4160 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4161 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 4162 | * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4163 | * @mtd: mtd info structure |
| 4164 | * @chip: nand chip info structure |
| 4165 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 4166 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 4167 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4168 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4169 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 4170 | * need a special oob layout and handling. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4171 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 4172 | static int nand_write_page_syndrome(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 4173 | struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 4174 | const uint8_t *buf, int oob_required, |
| 4175 | int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4176 | { |
| 4177 | int i, eccsize = chip->ecc.size; |
| 4178 | int eccbytes = chip->ecc.bytes; |
| 4179 | int eccsteps = chip->ecc.steps; |
| 4180 | const uint8_t *p = buf; |
| 4181 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4182 | int ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4183 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 4184 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 4185 | if (ret) |
| 4186 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4187 | |
| 4188 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4189 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4190 | |
| 4191 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 4192 | if (ret) |
| 4193 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4194 | |
| 4195 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4196 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 4197 | false); |
| 4198 | if (ret) |
| 4199 | return ret; |
| 4200 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4201 | oob += chip->ecc.prepad; |
| 4202 | } |
| 4203 | |
| 4204 | chip->ecc.calculate(mtd, p, oob); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4205 | |
| 4206 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 4207 | if (ret) |
| 4208 | return ret; |
| 4209 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4210 | oob += eccbytes; |
| 4211 | |
| 4212 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4213 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 4214 | false); |
| 4215 | if (ret) |
| 4216 | return ret; |
| 4217 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4218 | oob += chip->ecc.postpad; |
| 4219 | } |
| 4220 | } |
| 4221 | |
| 4222 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 4223 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4224 | if (i) { |
| 4225 | ret = nand_write_data_op(chip, oob, i, false); |
| 4226 | if (ret) |
| 4227 | return ret; |
| 4228 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 4229 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 4230 | return nand_prog_page_end_op(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4231 | } |
| 4232 | |
| 4233 | /** |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 4234 | * nand_write_page - write one page |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4235 | * @mtd: MTD device structure |
| 4236 | * @chip: NAND chip descriptor |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4237 | * @offset: address offset within the page |
| 4238 | * @data_len: length of actual data to be written |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4239 | * @buf: the data to write |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 4240 | * @oob_required: must write chip->oob_poi to OOB |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4241 | * @page: page number to write |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4242 | * @raw: use _raw version of write_page |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4243 | */ |
| 4244 | static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip, |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4245 | uint32_t offset, int data_len, const uint8_t *buf, |
Boris Brezillon | 0b4773fd | 2017-05-16 00:17:41 +0200 | [diff] [blame] | 4246 | int oob_required, int page, int raw) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4247 | { |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4248 | int status, subpage; |
| 4249 | |
| 4250 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && |
| 4251 | chip->ecc.write_subpage) |
| 4252 | subpage = offset || (data_len < mtd->writesize); |
| 4253 | else |
| 4254 | subpage = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4255 | |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 4256 | if (unlikely(raw)) |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4257 | status = chip->ecc.write_page_raw(mtd, chip, buf, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 4258 | oob_required, page); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4259 | else if (subpage) |
| 4260 | status = chip->ecc.write_subpage(mtd, chip, offset, data_len, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 4261 | buf, oob_required, page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 4262 | else |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 4263 | status = chip->ecc.write_page(mtd, chip, buf, oob_required, |
| 4264 | page); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 4265 | |
| 4266 | if (status < 0) |
| 4267 | return status; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4268 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4269 | return 0; |
| 4270 | } |
| 4271 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4272 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 4273 | * nand_fill_oob - [INTERN] Transfer client buffer to oob |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 4274 | * @mtd: MTD device structure |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4275 | * @oob: oob data buffer |
| 4276 | * @len: oob data write length |
| 4277 | * @ops: oob ops structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4278 | */ |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 4279 | static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len, |
| 4280 | struct mtd_oob_ops *ops) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4281 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4282 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4283 | int ret; |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 4284 | |
| 4285 | /* |
| 4286 | * Initialise to all 0xFF, to avoid the possibility of left over OOB |
| 4287 | * data from a previous OOB read. |
| 4288 | */ |
| 4289 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
| 4290 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 4291 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4292 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 4293 | case MTD_OPS_PLACE_OOB: |
| 4294 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4295 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); |
| 4296 | return oob + len; |
| 4297 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4298 | case MTD_OPS_AUTO_OOB: |
| 4299 | ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi, |
| 4300 | ops->ooboffs, len); |
| 4301 | BUG_ON(ret); |
| 4302 | return oob + len; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4303 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4304 | default: |
| 4305 | BUG(); |
| 4306 | } |
| 4307 | return NULL; |
| 4308 | } |
| 4309 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 4310 | #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4311 | |
| 4312 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 4313 | * nand_do_write_ops - [INTERN] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4314 | * @mtd: MTD device structure |
| 4315 | * @to: offset to write to |
| 4316 | * @ops: oob operations description structure |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4317 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4318 | * NAND write with ECC. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4319 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4320 | static int nand_do_write_ops(struct mtd_info *mtd, loff_t to, |
| 4321 | struct mtd_oob_ops *ops) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4322 | { |
Corentin Labbe | 73600b6 | 2017-09-02 10:49:38 +0200 | [diff] [blame] | 4323 | int chipnr, realpage, page, column; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4324 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4325 | uint32_t writelen = ops->len; |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4326 | |
| 4327 | uint32_t oobwritelen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 4328 | uint32_t oobmaxlen = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4329 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4330 | uint8_t *oob = ops->oobbuf; |
| 4331 | uint8_t *buf = ops->datbuf; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4332 | int ret; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 4333 | int oob_required = oob ? 1 : 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4334 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4335 | ops->retlen = 0; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4336 | if (!writelen) |
| 4337 | return 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4338 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4339 | /* Reject writes, which are not page aligned */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4340 | if (NOTALIGNED(to) || NOTALIGNED(ops->len)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 4341 | pr_notice("%s: attempt to write non page aligned data\n", |
| 4342 | __func__); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4343 | return -EINVAL; |
| 4344 | } |
| 4345 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4346 | column = to & (mtd->writesize - 1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4347 | |
Thomas Gleixner | 6a93096 | 2006-06-28 00:11:45 +0200 | [diff] [blame] | 4348 | chipnr = (int)(to >> chip->chip_shift); |
| 4349 | chip->select_chip(mtd, chipnr); |
| 4350 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4351 | /* Check, if it is write protected */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 4352 | if (nand_check_wp(mtd)) { |
| 4353 | ret = -EIO; |
| 4354 | goto err_out; |
| 4355 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4356 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4357 | realpage = (int)(to >> chip->page_shift); |
| 4358 | page = realpage & chip->pagemask; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4359 | |
| 4360 | /* Invalidate the page cache, when we write to the cached page */ |
Brian Norris | 537ab1b | 2014-07-21 19:08:03 -0700 | [diff] [blame] | 4361 | if (to <= ((loff_t)chip->pagebuf << chip->page_shift) && |
| 4362 | ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len)) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4363 | chip->pagebuf = -1; |
| 4364 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4365 | /* Don't allow multipage oob writes with offset */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 4366 | if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) { |
| 4367 | ret = -EINVAL; |
| 4368 | goto err_out; |
| 4369 | } |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4370 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 4371 | while (1) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4372 | int bytes = mtd->writesize; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4373 | uint8_t *wbuf = buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 4374 | int use_bufpoi; |
Hector Palacios | 144f4c9 | 2016-07-18 10:39:18 +0200 | [diff] [blame] | 4375 | int part_pagewr = (column || writelen < mtd->writesize); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4376 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 4377 | if (part_pagewr) |
| 4378 | use_bufpoi = 1; |
| 4379 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 4380 | use_bufpoi = !virt_addr_valid(buf) || |
| 4381 | !IS_ALIGNED((unsigned long)buf, |
| 4382 | chip->buf_align); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 4383 | else |
| 4384 | use_bufpoi = 0; |
| 4385 | |
| 4386 | /* Partial page write?, or need to use bounce buffer */ |
| 4387 | if (use_bufpoi) { |
| 4388 | pr_debug("%s: using write bounce buffer for buf@%p\n", |
| 4389 | __func__, buf); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 4390 | if (part_pagewr) |
| 4391 | bytes = min_t(int, bytes - column, writelen); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4392 | chip->pagebuf = -1; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 4393 | memset(chip->data_buf, 0xff, mtd->writesize); |
| 4394 | memcpy(&chip->data_buf[column], buf, bytes); |
| 4395 | wbuf = chip->data_buf; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4396 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4397 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4398 | if (unlikely(oob)) { |
| 4399 | size_t len = min(oobwritelen, oobmaxlen); |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 4400 | oob = nand_fill_oob(mtd, oob, len, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4401 | oobwritelen -= len; |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 4402 | } else { |
| 4403 | /* We still need to erase leftover OOB data */ |
| 4404 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4405 | } |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 4406 | |
| 4407 | ret = nand_write_page(mtd, chip, column, bytes, wbuf, |
Boris Brezillon | 0b4773fd | 2017-05-16 00:17:41 +0200 | [diff] [blame] | 4408 | oob_required, page, |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 4409 | (ops->mode == MTD_OPS_RAW)); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4410 | if (ret) |
| 4411 | break; |
| 4412 | |
| 4413 | writelen -= bytes; |
| 4414 | if (!writelen) |
| 4415 | break; |
| 4416 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4417 | column = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4418 | buf += bytes; |
| 4419 | realpage++; |
| 4420 | |
| 4421 | page = realpage & chip->pagemask; |
| 4422 | /* Check, if we cross a chip boundary */ |
| 4423 | if (!page) { |
| 4424 | chipnr++; |
| 4425 | chip->select_chip(mtd, -1); |
| 4426 | chip->select_chip(mtd, chipnr); |
| 4427 | } |
| 4428 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4429 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4430 | ops->retlen = ops->len - writelen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 4431 | if (unlikely(oob)) |
| 4432 | ops->oobretlen = ops->ooblen; |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 4433 | |
| 4434 | err_out: |
| 4435 | chip->select_chip(mtd, -1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4436 | return ret; |
| 4437 | } |
| 4438 | |
| 4439 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4440 | * panic_nand_write - [MTD Interface] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4441 | * @mtd: MTD device structure |
| 4442 | * @to: offset to write to |
| 4443 | * @len: number of bytes to write |
| 4444 | * @retlen: pointer to variable to store the number of written bytes |
| 4445 | * @buf: the data to write |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4446 | * |
| 4447 | * NAND write with ECC. Used when performing writes in interrupt context, this |
| 4448 | * may for example be called by mtdoops when writing an oops while in panic. |
| 4449 | */ |
| 4450 | static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 4451 | size_t *retlen, const uint8_t *buf) |
| 4452 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4453 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brent Taylor | 30863e38 | 2017-10-30 22:32:45 -0500 | [diff] [blame] | 4454 | int chipnr = (int)(to >> chip->chip_shift); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 4455 | struct mtd_oob_ops ops; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4456 | int ret; |
| 4457 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4458 | /* Grab the device */ |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4459 | panic_nand_get_device(chip, mtd, FL_WRITING); |
| 4460 | |
Brent Taylor | 30863e38 | 2017-10-30 22:32:45 -0500 | [diff] [blame] | 4461 | chip->select_chip(mtd, chipnr); |
| 4462 | |
| 4463 | /* Wait for the device to get ready */ |
| 4464 | panic_nand_wait(mtd, chip, 400); |
| 4465 | |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 4466 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 4467 | ops.len = len; |
| 4468 | ops.datbuf = (uint8_t *)buf; |
Huang Shijie | 11041ae6 | 2012-07-03 16:44:14 +0800 | [diff] [blame] | 4469 | ops.mode = MTD_OPS_PLACE_OOB; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4470 | |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 4471 | ret = nand_do_write_ops(mtd, to, &ops); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4472 | |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 4473 | *retlen = ops.retlen; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4474 | return ret; |
| 4475 | } |
| 4476 | |
| 4477 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4478 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4479 | * @mtd: MTD device structure |
| 4480 | * @to: offset to write to |
| 4481 | * @ops: oob operation description structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4482 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4483 | * NAND write out-of-band. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4484 | */ |
| 4485 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 4486 | struct mtd_oob_ops *ops) |
| 4487 | { |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 4488 | int chipnr, page, status, len; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4489 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4490 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4491 | pr_debug("%s: to = 0x%08x, len = %i\n", |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 4492 | __func__, (unsigned int)to, (int)ops->ooblen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4493 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 4494 | len = mtd_oobavail(mtd, ops); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 4495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | /* Do not allow write past end of page */ |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 4497 | if ((ops->ooboffs + ops->ooblen) > len) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4498 | pr_debug("%s: attempt to write past end of page\n", |
| 4499 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4500 | return -EINVAL; |
| 4501 | } |
| 4502 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 4503 | chipnr = (int)(to >> chip->chip_shift); |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 4504 | |
| 4505 | /* |
| 4506 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one |
| 4507 | * of my DiskOnChip 2000 test units) will clear the whole data page too |
| 4508 | * if we don't do this. I have no clue why, but I seem to have 'fixed' |
| 4509 | * it in the doc2000 driver in August 1999. dwmw2. |
| 4510 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4511 | nand_reset(chip, chipnr); |
| 4512 | |
| 4513 | chip->select_chip(mtd, chipnr); |
| 4514 | |
| 4515 | /* Shift to get page */ |
| 4516 | page = (int)(to >> chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4517 | |
| 4518 | /* Check, if it is write protected */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 4519 | if (nand_check_wp(mtd)) { |
| 4520 | chip->select_chip(mtd, -1); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4521 | return -EROFS; |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 4522 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4524 | /* Invalidate the page cache, if we write to the cached page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4525 | if (page == chip->pagebuf) |
| 4526 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4527 | |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 4528 | nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops); |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 4529 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 4530 | if (ops->mode == MTD_OPS_RAW) |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 4531 | status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask); |
| 4532 | else |
| 4533 | status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4534 | |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 4535 | chip->select_chip(mtd, -1); |
| 4536 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 4537 | if (status) |
| 4538 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4539 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 4540 | ops->oobretlen = ops->ooblen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4541 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 4542 | return 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4543 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4545 | /** |
| 4546 | * 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] | 4547 | * @mtd: MTD device structure |
| 4548 | * @to: offset to write to |
| 4549 | * @ops: oob operation description structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4550 | */ |
| 4551 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, |
| 4552 | struct mtd_oob_ops *ops) |
| 4553 | { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4554 | int ret = -ENOTSUPP; |
| 4555 | |
| 4556 | ops->retlen = 0; |
| 4557 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 4558 | nand_get_device(mtd, FL_WRITING); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4559 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 4560 | switch (ops->mode) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 4561 | case MTD_OPS_PLACE_OOB: |
| 4562 | case MTD_OPS_AUTO_OOB: |
| 4563 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4564 | break; |
| 4565 | |
| 4566 | default: |
| 4567 | goto out; |
| 4568 | } |
| 4569 | |
| 4570 | if (!ops->datbuf) |
| 4571 | ret = nand_do_write_oob(mtd, to, ops); |
| 4572 | else |
| 4573 | ret = nand_do_write_ops(mtd, to, ops); |
| 4574 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4575 | out: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4576 | nand_release_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4577 | return ret; |
| 4578 | } |
| 4579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4580 | /** |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4581 | * single_erase - [GENERIC] NAND standard block erase command function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4582 | * @mtd: MTD device structure |
| 4583 | * @page: the page address of the block which will be erased |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4584 | * |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4585 | * Standard erase command for NAND chips. Returns NAND status. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4586 | */ |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4587 | static int single_erase(struct mtd_info *mtd, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4588 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4589 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4590 | unsigned int eraseblock; |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4592 | /* Send commands to erase a block */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4593 | eraseblock = page >> (chip->phys_erase_shift - chip->page_shift); |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4594 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4595 | return nand_erase_op(chip, eraseblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4596 | } |
| 4597 | |
| 4598 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4599 | * nand_erase - [MTD Interface] erase block(s) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4600 | * @mtd: MTD device structure |
| 4601 | * @instr: erase instruction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4602 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4603 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4604 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4605 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4606 | { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4607 | return nand_erase_nand(mtd, instr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4608 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4610 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 4611 | * nand_erase_nand - [INTERN] erase block(s) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4612 | * @mtd: MTD device structure |
| 4613 | * @instr: erase instruction |
| 4614 | * @allowbbt: allow erasing the bbt area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4615 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4616 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4617 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4618 | int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, |
| 4619 | int allowbbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4620 | { |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4621 | int page, status, pages_per_block, ret, chipnr; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4622 | struct nand_chip *chip = mtd_to_nand(mtd); |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4623 | loff_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4624 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4625 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
| 4626 | __func__, (unsigned long long)instr->addr, |
| 4627 | (unsigned long long)instr->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4628 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 4629 | if (check_offs_len(mtd, instr->addr, instr->len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4630 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4632 | /* Grab the lock and see if the device is available */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 4633 | nand_get_device(mtd, FL_ERASING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4634 | |
| 4635 | /* Shift to get first page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4636 | page = (int)(instr->addr >> chip->page_shift); |
| 4637 | chipnr = (int)(instr->addr >> chip->chip_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4638 | |
| 4639 | /* Calculate pages in each block */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4640 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4641 | |
| 4642 | /* Select the NAND device */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4643 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4645 | /* Check, if it is write protected */ |
| 4646 | if (nand_check_wp(mtd)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4647 | pr_debug("%s: device is write protected!\n", |
| 4648 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4649 | instr->state = MTD_ERASE_FAILED; |
| 4650 | goto erase_exit; |
| 4651 | } |
| 4652 | |
| 4653 | /* Loop through the pages */ |
| 4654 | len = instr->len; |
| 4655 | |
| 4656 | instr->state = MTD_ERASING; |
| 4657 | |
| 4658 | while (len) { |
Wolfram Sang | 12183a2 | 2011-12-21 23:01:20 +0100 | [diff] [blame] | 4659 | /* Check if we have a bad block, we do not erase bad blocks! */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4660 | if (nand_block_checkbad(mtd, ((loff_t) page) << |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4661 | chip->page_shift, allowbbt)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 4662 | pr_warn("%s: attempt to erase a bad block at page 0x%08x\n", |
| 4663 | __func__, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4664 | instr->state = MTD_ERASE_FAILED; |
| 4665 | goto erase_exit; |
| 4666 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4667 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4668 | /* |
| 4669 | * Invalidate the page cache, if we erase the block which |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4670 | * contains the current cached page. |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4671 | */ |
| 4672 | if (page <= chip->pagebuf && chip->pagebuf < |
| 4673 | (page + pages_per_block)) |
| 4674 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4675 | |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4676 | status = chip->erase(mtd, page & chip->pagemask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4677 | |
| 4678 | /* See if block erase succeeded */ |
Miquel Raynal | eb94555 | 2017-11-30 18:01:28 +0100 | [diff] [blame] | 4679 | if (status) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4680 | pr_debug("%s: failed erase, page 0x%08x\n", |
| 4681 | __func__, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4682 | instr->state = MTD_ERASE_FAILED; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4683 | instr->fail_addr = |
| 4684 | ((loff_t)page << chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4685 | goto erase_exit; |
| 4686 | } |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 4687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4688 | /* Increment page address and decrement length */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 4689 | len -= (1ULL << chip->phys_erase_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4690 | page += pages_per_block; |
| 4691 | |
| 4692 | /* Check, if we cross a chip boundary */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4693 | if (len && !(page & chip->pagemask)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4694 | chipnr++; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4695 | chip->select_chip(mtd, -1); |
| 4696 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4697 | } |
| 4698 | } |
| 4699 | instr->state = MTD_ERASE_DONE; |
| 4700 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4701 | erase_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4702 | |
| 4703 | ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4704 | |
| 4705 | /* Deselect and wake up anyone waiting on the device */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 4706 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4707 | nand_release_device(mtd); |
| 4708 | |
David Woodhouse | 49defc0 | 2007-10-06 15:01:59 -0400 | [diff] [blame] | 4709 | /* Do call back function */ |
| 4710 | if (!ret) |
| 4711 | mtd_erase_callback(instr); |
| 4712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4713 | /* Return more or less happy */ |
| 4714 | return ret; |
| 4715 | } |
| 4716 | |
| 4717 | /** |
| 4718 | * nand_sync - [MTD Interface] sync |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4719 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4720 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4721 | * Sync is actually a wait for chip ready function. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4722 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4723 | static void nand_sync(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 | { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4725 | pr_debug("%s: called\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4726 | |
| 4727 | /* Grab the lock and see if the device is available */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 4728 | nand_get_device(mtd, FL_SYNCING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4729 | /* Release it and go back */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4730 | nand_release_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4731 | } |
| 4732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4733 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4734 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4735 | * @mtd: MTD device structure |
| 4736 | * @offs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4737 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4738 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4739 | { |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4740 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 4741 | int chipnr = (int)(offs >> chip->chip_shift); |
| 4742 | int ret; |
| 4743 | |
| 4744 | /* Select the NAND device */ |
| 4745 | nand_get_device(mtd, FL_READING); |
| 4746 | chip->select_chip(mtd, chipnr); |
| 4747 | |
| 4748 | ret = nand_block_checkbad(mtd, offs, 0); |
| 4749 | |
| 4750 | chip->select_chip(mtd, -1); |
| 4751 | nand_release_device(mtd); |
| 4752 | |
| 4753 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | } |
| 4755 | |
| 4756 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4757 | * 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] | 4758 | * @mtd: MTD device structure |
| 4759 | * @ofs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4760 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4761 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4762 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4763 | int ret; |
| 4764 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 4765 | ret = nand_block_isbad(mtd, ofs); |
| 4766 | if (ret) { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4767 | /* If it was bad already, return success and do nothing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4768 | if (ret > 0) |
| 4769 | return 0; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4770 | return ret; |
| 4771 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4772 | |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 4773 | return nand_block_markbad_lowlevel(mtd, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4774 | } |
| 4775 | |
| 4776 | /** |
Zach Brown | 5671842 | 2017-01-10 13:30:20 -0600 | [diff] [blame] | 4777 | * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd |
| 4778 | * @mtd: MTD device structure |
| 4779 | * @ofs: offset relative to mtd start |
| 4780 | * @len: length of mtd |
| 4781 | */ |
| 4782 | static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len) |
| 4783 | { |
| 4784 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 4785 | u32 part_start_block; |
| 4786 | u32 part_end_block; |
| 4787 | u32 part_start_die; |
| 4788 | u32 part_end_die; |
| 4789 | |
| 4790 | /* |
| 4791 | * max_bb_per_die and blocks_per_die used to determine |
| 4792 | * the maximum bad block count. |
| 4793 | */ |
| 4794 | if (!chip->max_bb_per_die || !chip->blocks_per_die) |
| 4795 | return -ENOTSUPP; |
| 4796 | |
| 4797 | /* Get the start and end of the partition in erase blocks. */ |
| 4798 | part_start_block = mtd_div_by_eb(ofs, mtd); |
| 4799 | part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1; |
| 4800 | |
| 4801 | /* Get the start and end LUNs of the partition. */ |
| 4802 | part_start_die = part_start_block / chip->blocks_per_die; |
| 4803 | part_end_die = part_end_block / chip->blocks_per_die; |
| 4804 | |
| 4805 | /* |
| 4806 | * Look up the bad blocks per unit and multiply by the number of units |
| 4807 | * that the partition spans. |
| 4808 | */ |
| 4809 | return chip->max_bb_per_die * (part_end_die - part_start_die + 1); |
| 4810 | } |
| 4811 | |
| 4812 | /** |
Miquel Raynal | b958758 | 2018-03-19 14:47:19 +0100 | [diff] [blame] | 4813 | * nand_default_set_features- [REPLACEABLE] set NAND chip features |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 4814 | * @mtd: MTD device structure |
| 4815 | * @chip: nand chip info structure |
| 4816 | * @addr: feature address. |
| 4817 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 4818 | */ |
Miquel Raynal | b958758 | 2018-03-19 14:47:19 +0100 | [diff] [blame] | 4819 | static int nand_default_set_features(struct mtd_info *mtd, |
| 4820 | struct nand_chip *chip, int addr, |
| 4821 | uint8_t *subfeature_param) |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 4822 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4823 | return nand_set_features_op(chip, addr, subfeature_param); |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 4824 | } |
| 4825 | |
| 4826 | /** |
Miquel Raynal | b958758 | 2018-03-19 14:47:19 +0100 | [diff] [blame] | 4827 | * nand_default_get_features- [REPLACEABLE] get NAND chip features |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 4828 | * @mtd: MTD device structure |
| 4829 | * @chip: nand chip info structure |
| 4830 | * @addr: feature address. |
| 4831 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 4832 | */ |
Miquel Raynal | b958758 | 2018-03-19 14:47:19 +0100 | [diff] [blame] | 4833 | static int nand_default_get_features(struct mtd_info *mtd, |
| 4834 | struct nand_chip *chip, int addr, |
| 4835 | uint8_t *subfeature_param) |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 4836 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4837 | return nand_get_features_op(chip, addr, subfeature_param); |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 4838 | } |
| 4839 | |
| 4840 | /** |
Miquel Raynal | b958758 | 2018-03-19 14:47:19 +0100 | [diff] [blame] | 4841 | * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP |
Boris Brezillon | 4a78cc6 | 2017-05-26 17:10:15 +0200 | [diff] [blame] | 4842 | * @mtd: MTD device structure |
| 4843 | * @chip: nand chip info structure |
| 4844 | * @addr: feature address. |
| 4845 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 4846 | * |
| 4847 | * Should be used by NAND controller drivers that do not support the SET/GET |
| 4848 | * FEATURES operations. |
| 4849 | */ |
Miquel Raynal | b958758 | 2018-03-19 14:47:19 +0100 | [diff] [blame] | 4850 | int nand_get_set_features_notsupp(struct mtd_info *mtd, struct nand_chip *chip, |
| 4851 | int addr, u8 *subfeature_param) |
Boris Brezillon | 4a78cc6 | 2017-05-26 17:10:15 +0200 | [diff] [blame] | 4852 | { |
| 4853 | return -ENOTSUPP; |
| 4854 | } |
Miquel Raynal | b958758 | 2018-03-19 14:47:19 +0100 | [diff] [blame] | 4855 | EXPORT_SYMBOL(nand_get_set_features_notsupp); |
Boris Brezillon | 4a78cc6 | 2017-05-26 17:10:15 +0200 | [diff] [blame] | 4856 | |
| 4857 | /** |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4858 | * nand_suspend - [MTD Interface] Suspend the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4859 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4860 | */ |
| 4861 | static int nand_suspend(struct mtd_info *mtd) |
| 4862 | { |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 4863 | return nand_get_device(mtd, FL_PM_SUSPENDED); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4864 | } |
| 4865 | |
| 4866 | /** |
| 4867 | * nand_resume - [MTD Interface] Resume the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4868 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4869 | */ |
| 4870 | static void nand_resume(struct mtd_info *mtd) |
| 4871 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4872 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4873 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4874 | if (chip->state == FL_PM_SUSPENDED) |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4875 | nand_release_device(mtd); |
| 4876 | else |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 4877 | pr_err("%s called for a chip which is not in suspended state\n", |
| 4878 | __func__); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4879 | } |
| 4880 | |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 4881 | /** |
| 4882 | * nand_shutdown - [MTD Interface] Finish the current NAND operation and |
| 4883 | * prevent further operations |
| 4884 | * @mtd: MTD device structure |
| 4885 | */ |
| 4886 | static void nand_shutdown(struct mtd_info *mtd) |
| 4887 | { |
Brian Norris | 9ca641b | 2015-11-09 16:37:28 -0800 | [diff] [blame] | 4888 | nand_get_device(mtd, FL_PM_SUSPENDED); |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 4889 | } |
| 4890 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4891 | /* Set default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4892 | static void nand_set_defaults(struct nand_chip *chip) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4893 | { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4894 | unsigned int busw = chip->options & NAND_BUSWIDTH_16; |
| 4895 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4896 | /* check for proper chip_delay setup, set 20us if not */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4897 | if (!chip->chip_delay) |
| 4898 | chip->chip_delay = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4899 | |
| 4900 | /* check, if a user supplied command function given */ |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 4901 | if (!chip->cmdfunc && !chip->exec_op) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4902 | chip->cmdfunc = nand_command; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4903 | |
| 4904 | /* check, if a user supplied wait function given */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4905 | if (chip->waitfunc == NULL) |
| 4906 | chip->waitfunc = nand_wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4907 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4908 | if (!chip->select_chip) |
| 4909 | chip->select_chip = nand_select_chip; |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 4910 | |
Huang Shijie | 4204ccc | 2013-08-16 10:10:07 +0800 | [diff] [blame] | 4911 | /* set for ONFI nand */ |
Miquel Raynal | b958758 | 2018-03-19 14:47:19 +0100 | [diff] [blame] | 4912 | if (!chip->set_features) |
| 4913 | chip->set_features = nand_default_set_features; |
| 4914 | if (!chip->get_features) |
| 4915 | chip->get_features = nand_default_get_features; |
Huang Shijie | 4204ccc | 2013-08-16 10:10:07 +0800 | [diff] [blame] | 4916 | |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 4917 | /* If called twice, pointers that depend on busw may need to be reset */ |
| 4918 | if (!chip->read_byte || chip->read_byte == nand_read_byte) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4919 | chip->read_byte = busw ? nand_read_byte16 : nand_read_byte; |
| 4920 | if (!chip->read_word) |
| 4921 | chip->read_word = nand_read_word; |
| 4922 | if (!chip->block_bad) |
| 4923 | chip->block_bad = nand_block_bad; |
| 4924 | if (!chip->block_markbad) |
| 4925 | chip->block_markbad = nand_default_block_markbad; |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 4926 | if (!chip->write_buf || chip->write_buf == nand_write_buf) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4927 | chip->write_buf = busw ? nand_write_buf16 : nand_write_buf; |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 4928 | if (!chip->write_byte || chip->write_byte == nand_write_byte) |
| 4929 | chip->write_byte = busw ? nand_write_byte16 : nand_write_byte; |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 4930 | if (!chip->read_buf || chip->read_buf == nand_read_buf) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4931 | chip->read_buf = busw ? nand_read_buf16 : nand_read_buf; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4932 | if (!chip->scan_bbt) |
| 4933 | chip->scan_bbt = nand_default_bbt; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4934 | |
| 4935 | if (!chip->controller) { |
| 4936 | chip->controller = &chip->hwcontrol; |
Marc Gonzalez | d45bc58 | 2016-07-27 11:23:52 +0200 | [diff] [blame] | 4937 | nand_hw_control_init(chip->controller); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4938 | } |
| 4939 | |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 4940 | if (!chip->buf_align) |
| 4941 | chip->buf_align = 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4942 | } |
| 4943 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4944 | /* Sanitize ONFI strings so we can safely print them */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4945 | static void sanitize_string(uint8_t *s, size_t len) |
| 4946 | { |
| 4947 | ssize_t i; |
| 4948 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4949 | /* Null terminate */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4950 | s[len - 1] = 0; |
| 4951 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4952 | /* Remove non printable chars */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4953 | for (i = 0; i < len - 1; i++) { |
| 4954 | if (s[i] < ' ' || s[i] > 127) |
| 4955 | s[i] = '?'; |
| 4956 | } |
| 4957 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4958 | /* Remove trailing spaces */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4959 | strim(s); |
| 4960 | } |
| 4961 | |
| 4962 | static u16 onfi_crc16(u16 crc, u8 const *p, size_t len) |
| 4963 | { |
| 4964 | int i; |
| 4965 | while (len--) { |
| 4966 | crc ^= *p++ << 8; |
| 4967 | for (i = 0; i < 8; i++) |
| 4968 | crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0); |
| 4969 | } |
| 4970 | |
| 4971 | return crc; |
| 4972 | } |
| 4973 | |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 4974 | /* Parse the Extended Parameter Page. */ |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4975 | static int nand_flash_detect_ext_param_page(struct nand_chip *chip, |
| 4976 | struct nand_onfi_params *p) |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 4977 | { |
| 4978 | struct onfi_ext_param_page *ep; |
| 4979 | struct onfi_ext_section *s; |
| 4980 | struct onfi_ext_ecc_info *ecc; |
| 4981 | uint8_t *cursor; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4982 | int ret; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 4983 | int len; |
| 4984 | int i; |
| 4985 | |
| 4986 | len = le16_to_cpu(p->ext_param_page_length) * 16; |
| 4987 | ep = kmalloc(len, GFP_KERNEL); |
Brian Norris | 5cb1327 | 2013-09-16 17:59:20 -0700 | [diff] [blame] | 4988 | if (!ep) |
| 4989 | return -ENOMEM; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 4990 | |
| 4991 | /* Send our own NAND_CMD_PARAM. */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4992 | ret = nand_read_param_page_op(chip, 0, NULL, 0); |
| 4993 | if (ret) |
| 4994 | goto ext_out; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 4995 | |
| 4996 | /* Use the Change Read Column command to skip the ONFI param pages. */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4997 | ret = nand_change_read_column_op(chip, |
| 4998 | sizeof(*p) * p->num_of_param_pages, |
| 4999 | ep, len, true); |
| 5000 | if (ret) |
| 5001 | goto ext_out; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 5002 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5003 | ret = -EINVAL; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 5004 | if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2) |
| 5005 | != le16_to_cpu(ep->crc))) { |
| 5006 | pr_debug("fail in the CRC.\n"); |
| 5007 | goto ext_out; |
| 5008 | } |
| 5009 | |
| 5010 | /* |
| 5011 | * Check the signature. |
| 5012 | * Do not strictly follow the ONFI spec, maybe changed in future. |
| 5013 | */ |
| 5014 | if (strncmp(ep->sig, "EPPS", 4)) { |
| 5015 | pr_debug("The signature is invalid.\n"); |
| 5016 | goto ext_out; |
| 5017 | } |
| 5018 | |
| 5019 | /* find the ECC section. */ |
| 5020 | cursor = (uint8_t *)(ep + 1); |
| 5021 | for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) { |
| 5022 | s = ep->sections + i; |
| 5023 | if (s->type == ONFI_SECTION_TYPE_2) |
| 5024 | break; |
| 5025 | cursor += s->length * 16; |
| 5026 | } |
| 5027 | if (i == ONFI_EXT_SECTION_MAX) { |
| 5028 | pr_debug("We can not find the ECC section.\n"); |
| 5029 | goto ext_out; |
| 5030 | } |
| 5031 | |
| 5032 | /* get the info we want. */ |
| 5033 | ecc = (struct onfi_ext_ecc_info *)cursor; |
| 5034 | |
Brian Norris | 4ae7d22 | 2013-09-16 18:20:21 -0700 | [diff] [blame] | 5035 | if (!ecc->codeword_size) { |
| 5036 | pr_debug("Invalid codeword size\n"); |
| 5037 | goto ext_out; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 5038 | } |
| 5039 | |
Brian Norris | 4ae7d22 | 2013-09-16 18:20:21 -0700 | [diff] [blame] | 5040 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 5041 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
Brian Norris | 5cb1327 | 2013-09-16 17:59:20 -0700 | [diff] [blame] | 5042 | ret = 0; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 5043 | |
| 5044 | ext_out: |
| 5045 | kfree(ep); |
| 5046 | return ret; |
| 5047 | } |
| 5048 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 5049 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5050 | * Check if the NAND chip is ONFI compliant, returns 1 if it is, 0 otherwise. |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5051 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5052 | static int nand_flash_detect_onfi(struct nand_chip *chip) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5053 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 5054 | struct mtd_info *mtd = nand_to_mtd(chip); |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5055 | struct nand_onfi_params *p = &chip->onfi_params; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5056 | char id[4]; |
| 5057 | int i, ret, val; |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5058 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 5059 | /* Try ONFI for unknown chip or LP */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5060 | ret = nand_readid_op(chip, 0x20, id, sizeof(id)); |
| 5061 | if (ret || strncmp(id, "ONFI", 4)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5062 | return 0; |
| 5063 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5064 | ret = nand_read_param_page_op(chip, 0, NULL, 0); |
| 5065 | if (ret) |
| 5066 | return 0; |
| 5067 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5068 | for (i = 0; i < 3; i++) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5069 | ret = nand_read_data_op(chip, p, sizeof(*p), true); |
| 5070 | if (ret) |
| 5071 | return 0; |
| 5072 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5073 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) == |
| 5074 | le16_to_cpu(p->crc)) { |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5075 | break; |
| 5076 | } |
| 5077 | } |
| 5078 | |
Brian Norris | c7f23a7 | 2013-08-13 10:51:55 -0700 | [diff] [blame] | 5079 | if (i == 3) { |
| 5080 | pr_err("Could not find valid ONFI parameter page; aborting\n"); |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5081 | return 0; |
Brian Norris | c7f23a7 | 2013-08-13 10:51:55 -0700 | [diff] [blame] | 5082 | } |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5083 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5084 | /* Check version */ |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5085 | val = le16_to_cpu(p->revision); |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 5086 | if (val & (1 << 5)) |
| 5087 | chip->onfi_version = 23; |
| 5088 | else if (val & (1 << 4)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5089 | chip->onfi_version = 22; |
| 5090 | else if (val & (1 << 3)) |
| 5091 | chip->onfi_version = 21; |
| 5092 | else if (val & (1 << 2)) |
| 5093 | chip->onfi_version = 20; |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 5094 | else if (val & (1 << 1)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5095 | chip->onfi_version = 10; |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 5096 | |
| 5097 | if (!chip->onfi_version) { |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 5098 | pr_info("unsupported ONFI version: %d\n", val); |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 5099 | return 0; |
| 5100 | } |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5101 | |
| 5102 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 5103 | sanitize_string(p->model, sizeof(p->model)); |
| 5104 | if (!mtd->name) |
| 5105 | mtd->name = p->model; |
Brian Norris | 4355b70 | 2013-08-27 18:45:10 -0700 | [diff] [blame] | 5106 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5107 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
Brian Norris | 4355b70 | 2013-08-27 18:45:10 -0700 | [diff] [blame] | 5108 | |
| 5109 | /* |
| 5110 | * pages_per_block and blocks_per_lun may not be a power-of-2 size |
| 5111 | * (don't ask me who thought of this...). MTD assumes that these |
| 5112 | * dimensions will be power-of-2, so just truncate the remaining area. |
| 5113 | */ |
| 5114 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 5115 | mtd->erasesize *= mtd->writesize; |
| 5116 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5117 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
Brian Norris | 4355b70 | 2013-08-27 18:45:10 -0700 | [diff] [blame] | 5118 | |
| 5119 | /* See erasesize comment */ |
| 5120 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
Matthieu CASTET | 6379575 | 2012-03-19 15:35:25 +0100 | [diff] [blame] | 5121 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
Huang Shijie | 13fbd17 | 2013-09-25 14:58:13 +0800 | [diff] [blame] | 5122 | chip->bits_per_cell = p->bits_per_cell; |
Huang Shijie | e2985fc | 2013-05-17 11:17:30 +0800 | [diff] [blame] | 5123 | |
Zach Brown | 34da5f5 | 2017-01-10 13:30:21 -0600 | [diff] [blame] | 5124 | chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun); |
| 5125 | chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun); |
| 5126 | |
Huang Shijie | e2985fc | 2013-05-17 11:17:30 +0800 | [diff] [blame] | 5127 | if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS) |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5128 | chip->options |= NAND_BUSWIDTH_16; |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5129 | |
Huang Shijie | 10c86ba | 2013-05-17 11:17:26 +0800 | [diff] [blame] | 5130 | if (p->ecc_bits != 0xff) { |
| 5131 | chip->ecc_strength_ds = p->ecc_bits; |
| 5132 | chip->ecc_step_ds = 512; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 5133 | } else if (chip->onfi_version >= 21 && |
| 5134 | (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) { |
| 5135 | |
| 5136 | /* |
| 5137 | * The nand_flash_detect_ext_param_page() uses the |
| 5138 | * Change Read Column command which maybe not supported |
| 5139 | * by the chip->cmdfunc. So try to update the chip->cmdfunc |
| 5140 | * now. We do not replace user supplied command function. |
| 5141 | */ |
| 5142 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 5143 | chip->cmdfunc = nand_command_lp; |
| 5144 | |
| 5145 | /* The Extended Parameter Page is supported since ONFI 2.1. */ |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 5146 | if (nand_flash_detect_ext_param_page(chip, p)) |
Brian Norris | c7f23a7 | 2013-08-13 10:51:55 -0700 | [diff] [blame] | 5147 | pr_warn("Failed to detect ONFI extended param page\n"); |
| 5148 | } else { |
| 5149 | pr_warn("Could not retrieve ONFI ECC requirements\n"); |
Huang Shijie | 10c86ba | 2013-05-17 11:17:26 +0800 | [diff] [blame] | 5150 | } |
| 5151 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5152 | return 1; |
| 5153 | } |
| 5154 | |
| 5155 | /* |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5156 | * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise. |
| 5157 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5158 | static int nand_flash_detect_jedec(struct nand_chip *chip) |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5159 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 5160 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5161 | struct nand_jedec_params *p = &chip->jedec_params; |
| 5162 | struct jedec_ecc_info *ecc; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5163 | char id[5]; |
| 5164 | int i, val, ret; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5165 | |
| 5166 | /* Try JEDEC for unknown chip or LP */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5167 | ret = nand_readid_op(chip, 0x40, id, sizeof(id)); |
| 5168 | if (ret || strncmp(id, "JEDEC", sizeof(id))) |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5169 | return 0; |
| 5170 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5171 | ret = nand_read_param_page_op(chip, 0x40, NULL, 0); |
| 5172 | if (ret) |
| 5173 | return 0; |
| 5174 | |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5175 | for (i = 0; i < 3; i++) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5176 | ret = nand_read_data_op(chip, p, sizeof(*p), true); |
| 5177 | if (ret) |
| 5178 | return 0; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5179 | |
| 5180 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) == |
| 5181 | le16_to_cpu(p->crc)) |
| 5182 | break; |
| 5183 | } |
| 5184 | |
| 5185 | if (i == 3) { |
| 5186 | pr_err("Could not find valid JEDEC parameter page; aborting\n"); |
| 5187 | return 0; |
| 5188 | } |
| 5189 | |
| 5190 | /* Check version */ |
| 5191 | val = le16_to_cpu(p->revision); |
| 5192 | if (val & (1 << 2)) |
| 5193 | chip->jedec_version = 10; |
| 5194 | else if (val & (1 << 1)) |
| 5195 | chip->jedec_version = 1; /* vendor specific version */ |
| 5196 | |
| 5197 | if (!chip->jedec_version) { |
| 5198 | pr_info("unsupported JEDEC version: %d\n", val); |
| 5199 | return 0; |
| 5200 | } |
| 5201 | |
| 5202 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 5203 | sanitize_string(p->model, sizeof(p->model)); |
| 5204 | if (!mtd->name) |
| 5205 | mtd->name = p->model; |
| 5206 | |
| 5207 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
| 5208 | |
| 5209 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 5210 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 5211 | mtd->erasesize *= mtd->writesize; |
| 5212 | |
| 5213 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
| 5214 | |
| 5215 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 5216 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
| 5217 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
| 5218 | chip->bits_per_cell = p->bits_per_cell; |
| 5219 | |
| 5220 | if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS) |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5221 | chip->options |= NAND_BUSWIDTH_16; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5222 | |
| 5223 | /* ECC info */ |
| 5224 | ecc = &p->ecc_info[0]; |
| 5225 | |
| 5226 | if (ecc->codeword_size >= 9) { |
| 5227 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 5228 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
| 5229 | } else { |
| 5230 | pr_warn("Invalid codeword size\n"); |
| 5231 | } |
| 5232 | |
| 5233 | return 1; |
| 5234 | } |
| 5235 | |
| 5236 | /* |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 5237 | * nand_id_has_period - Check if an ID string has a given wraparound period |
| 5238 | * @id_data: the ID string |
| 5239 | * @arrlen: the length of the @id_data array |
| 5240 | * @period: the period of repitition |
| 5241 | * |
| 5242 | * Check if an ID string is repeated within a given sequence of bytes at |
| 5243 | * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a |
Brian Norris | d4d4f1b | 2012-11-14 21:54:20 -0800 | [diff] [blame] | 5244 | * 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] | 5245 | * if the repetition has a period of @period; otherwise, returns zero. |
| 5246 | */ |
| 5247 | static int nand_id_has_period(u8 *id_data, int arrlen, int period) |
| 5248 | { |
| 5249 | int i, j; |
| 5250 | for (i = 0; i < period; i++) |
| 5251 | for (j = i + period; j < arrlen; j += period) |
| 5252 | if (id_data[i] != id_data[j]) |
| 5253 | return 0; |
| 5254 | return 1; |
| 5255 | } |
| 5256 | |
| 5257 | /* |
| 5258 | * nand_id_len - Get the length of an ID string returned by CMD_READID |
| 5259 | * @id_data: the ID string |
| 5260 | * @arrlen: the length of the @id_data array |
| 5261 | |
| 5262 | * Returns the length of the ID string, according to known wraparound/trailing |
| 5263 | * zero patterns. If no pattern exists, returns the length of the array. |
| 5264 | */ |
| 5265 | static int nand_id_len(u8 *id_data, int arrlen) |
| 5266 | { |
| 5267 | int last_nonzero, period; |
| 5268 | |
| 5269 | /* Find last non-zero byte */ |
| 5270 | for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--) |
| 5271 | if (id_data[last_nonzero]) |
| 5272 | break; |
| 5273 | |
| 5274 | /* All zeros */ |
| 5275 | if (last_nonzero < 0) |
| 5276 | return 0; |
| 5277 | |
| 5278 | /* Calculate wraparound period */ |
| 5279 | for (period = 1; period < arrlen; period++) |
| 5280 | if (nand_id_has_period(id_data, arrlen, period)) |
| 5281 | break; |
| 5282 | |
| 5283 | /* There's a repeated pattern */ |
| 5284 | if (period < arrlen) |
| 5285 | return period; |
| 5286 | |
| 5287 | /* There are trailing zeros */ |
| 5288 | if (last_nonzero < arrlen - 1) |
| 5289 | return last_nonzero + 1; |
| 5290 | |
| 5291 | /* No pattern detected */ |
| 5292 | return arrlen; |
| 5293 | } |
| 5294 | |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 5295 | /* Extract the bits of per cell from the 3rd byte of the extended ID */ |
| 5296 | static int nand_get_bits_per_cell(u8 cellinfo) |
| 5297 | { |
| 5298 | int bits; |
| 5299 | |
| 5300 | bits = cellinfo & NAND_CI_CELLTYPE_MSK; |
| 5301 | bits >>= NAND_CI_CELLTYPE_SHIFT; |
| 5302 | return bits + 1; |
| 5303 | } |
| 5304 | |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 5305 | /* |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 5306 | * Many new NAND share similar device ID codes, which represent the size of the |
| 5307 | * chip. The rest of the parameters must be decoded according to generic or |
| 5308 | * manufacturer-specific "extended ID" decoding patterns. |
| 5309 | */ |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5310 | void nand_decode_ext_id(struct nand_chip *chip) |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 5311 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 5312 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 9b2d61f | 2016-06-08 10:34:57 +0200 | [diff] [blame] | 5313 | int extid; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 5314 | u8 *id_data = chip->id.data; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 5315 | /* The 3rd id byte holds MLC / multichip data */ |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 5316 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 5317 | /* The 4th id byte is the important one */ |
| 5318 | extid = id_data[3]; |
| 5319 | |
Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 5320 | /* Calc pagesize */ |
| 5321 | mtd->writesize = 1024 << (extid & 0x03); |
| 5322 | extid >>= 2; |
| 5323 | /* Calc oobsize */ |
| 5324 | mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9); |
| 5325 | extid >>= 2; |
| 5326 | /* Calc blocksize. Blocksize is multiples of 64KiB */ |
| 5327 | mtd->erasesize = (64 * 1024) << (extid & 0x03); |
| 5328 | extid >>= 2; |
| 5329 | /* Get buswidth information */ |
| 5330 | if (extid & 0x1) |
| 5331 | chip->options |= NAND_BUSWIDTH_16; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 5332 | } |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5333 | EXPORT_SYMBOL_GPL(nand_decode_ext_id); |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 5334 | |
| 5335 | /* |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 5336 | * Old devices have chip data hardcoded in the device ID table. nand_decode_id |
| 5337 | * decodes a matching ID table entry and assigns the MTD size parameters for |
| 5338 | * the chip. |
| 5339 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5340 | 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] | 5341 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 5342 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 5343 | |
| 5344 | mtd->erasesize = type->erasesize; |
| 5345 | mtd->writesize = type->pagesize; |
| 5346 | mtd->oobsize = mtd->writesize / 32; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 5347 | |
Huang Shijie | 1c195e9 | 2013-09-25 14:58:12 +0800 | [diff] [blame] | 5348 | /* All legacy ID NAND are small-page, SLC */ |
| 5349 | chip->bits_per_cell = 1; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 5350 | } |
| 5351 | |
| 5352 | /* |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 5353 | * Set the bad block marker/indicator (BBM/BBI) patterns according to some |
| 5354 | * heuristic patterns using various detected parameters (e.g., manufacturer, |
| 5355 | * page size, cell-type information). |
| 5356 | */ |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 5357 | static void nand_decode_bbm_options(struct nand_chip *chip) |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 5358 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 5359 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 5360 | |
| 5361 | /* Set the bad block position */ |
| 5362 | if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16)) |
| 5363 | chip->badblockpos = NAND_LARGE_BADBLOCK_POS; |
| 5364 | else |
| 5365 | chip->badblockpos = NAND_SMALL_BADBLOCK_POS; |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 5366 | } |
| 5367 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 5368 | static inline bool is_full_id_nand(struct nand_flash_dev *type) |
| 5369 | { |
| 5370 | return type->id_len; |
| 5371 | } |
| 5372 | |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 5373 | static bool find_full_id_nand(struct nand_chip *chip, |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5374 | struct nand_flash_dev *type) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 5375 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 5376 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 5377 | u8 *id_data = chip->id.data; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 5378 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 5379 | if (!strncmp(type->id, id_data, type->id_len)) { |
| 5380 | mtd->writesize = type->pagesize; |
| 5381 | mtd->erasesize = type->erasesize; |
| 5382 | mtd->oobsize = type->oobsize; |
| 5383 | |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 5384 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 5385 | chip->chipsize = (uint64_t)type->chipsize << 20; |
| 5386 | chip->options |= type->options; |
Huang Shijie | 5721934 | 2013-05-17 11:17:32 +0800 | [diff] [blame] | 5387 | chip->ecc_strength_ds = NAND_ECC_STRENGTH(type); |
| 5388 | chip->ecc_step_ds = NAND_ECC_STEP(type); |
Boris BREZILLON | 57a94e2 | 2014-09-22 20:11:50 +0200 | [diff] [blame] | 5389 | chip->onfi_timing_mode_default = |
| 5390 | type->onfi_timing_mode_default; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 5391 | |
Cai Zhiyong | 092b6a1 | 2013-12-25 21:19:21 +0800 | [diff] [blame] | 5392 | if (!mtd->name) |
| 5393 | mtd->name = type->name; |
| 5394 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 5395 | return true; |
| 5396 | } |
| 5397 | return false; |
| 5398 | } |
| 5399 | |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 5400 | /* |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5401 | * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC |
| 5402 | * compliant and does not have a full-id or legacy-id entry in the nand_ids |
| 5403 | * table. |
| 5404 | */ |
| 5405 | static void nand_manufacturer_detect(struct nand_chip *chip) |
| 5406 | { |
| 5407 | /* |
| 5408 | * Try manufacturer detection if available and use |
| 5409 | * nand_decode_ext_id() otherwise. |
| 5410 | */ |
| 5411 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 5412 | chip->manufacturer.desc->ops->detect) { |
| 5413 | /* The 3rd id byte holds MLC / multichip data */ |
| 5414 | 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] | 5415 | chip->manufacturer.desc->ops->detect(chip); |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 5416 | } else { |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5417 | nand_decode_ext_id(chip); |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 5418 | } |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5419 | } |
| 5420 | |
| 5421 | /* |
| 5422 | * Manufacturer initialization. This function is called for all NANDs including |
| 5423 | * ONFI and JEDEC compliant ones. |
| 5424 | * Manufacturer drivers should put all their specific initialization code in |
| 5425 | * their ->init() hook. |
| 5426 | */ |
| 5427 | static int nand_manufacturer_init(struct nand_chip *chip) |
| 5428 | { |
| 5429 | if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops || |
| 5430 | !chip->manufacturer.desc->ops->init) |
| 5431 | return 0; |
| 5432 | |
| 5433 | return chip->manufacturer.desc->ops->init(chip); |
| 5434 | } |
| 5435 | |
| 5436 | /* |
| 5437 | * Manufacturer cleanup. This function is called for all NANDs including |
| 5438 | * ONFI and JEDEC compliant ones. |
| 5439 | * Manufacturer drivers should put all their specific cleanup code in their |
| 5440 | * ->cleanup() hook. |
| 5441 | */ |
| 5442 | static void nand_manufacturer_cleanup(struct nand_chip *chip) |
| 5443 | { |
| 5444 | /* Release manufacturer private data */ |
| 5445 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
| 5446 | chip->manufacturer.desc->ops->cleanup) |
| 5447 | chip->manufacturer.desc->ops->cleanup(chip); |
| 5448 | } |
| 5449 | |
| 5450 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5451 | * 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] | 5452 | */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame] | 5453 | 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] | 5454 | { |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 5455 | const struct nand_manufacturer *manufacturer; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 5456 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5457 | int busw, ret; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 5458 | u8 *id_data = chip->id.data; |
| 5459 | u8 maf_id, dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5460 | |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 5461 | /* |
| 5462 | * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5463 | * after power-up. |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 5464 | */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5465 | ret = nand_reset(chip, 0); |
| 5466 | if (ret) |
| 5467 | return ret; |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 5468 | |
| 5469 | /* Select the device */ |
| 5470 | chip->select_chip(mtd, 0); |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 5471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5472 | /* Send the command for reading device ID */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5473 | ret = nand_readid_op(chip, 0, id_data, 2); |
| 5474 | if (ret) |
| 5475 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5476 | |
| 5477 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5478 | maf_id = id_data[0]; |
| 5479 | dev_id = id_data[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5480 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5481 | /* |
| 5482 | * 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] | 5483 | * interface concerns can cause random data which looks like a |
| 5484 | * possibly credible NAND flash to appear. If the two results do |
| 5485 | * not match, ignore the device completely. |
| 5486 | */ |
| 5487 | |
Brian Norris | 4aef9b7 | 2012-09-24 20:40:48 -0700 | [diff] [blame] | 5488 | /* Read entire ID string */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5489 | ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data)); |
| 5490 | if (ret) |
| 5491 | return ret; |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 5492 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 5493 | if (id_data[0] != maf_id || id_data[1] != dev_id) { |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 5494 | 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] | 5495 | maf_id, dev_id, id_data[0], id_data[1]); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 5496 | return -ENODEV; |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 5497 | } |
| 5498 | |
Jean-Louis Thekekara | 5158bd5 | 2017-06-29 19:08:30 +0200 | [diff] [blame] | 5499 | 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] | 5500 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5501 | /* Try to identify manufacturer */ |
| 5502 | manufacturer = nand_get_manufacturer(maf_id); |
| 5503 | chip->manufacturer.desc = manufacturer; |
| 5504 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5505 | if (!type) |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 5506 | type = nand_flash_ids; |
| 5507 | |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5508 | /* |
| 5509 | * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic |
| 5510 | * override it. |
| 5511 | * This is required to make sure initial NAND bus width set by the |
| 5512 | * NAND controller driver is coherent with the real NAND bus width |
| 5513 | * (extracted by auto-detection code). |
| 5514 | */ |
| 5515 | busw = chip->options & NAND_BUSWIDTH_16; |
| 5516 | |
| 5517 | /* |
| 5518 | * The flag is only set (never cleared), reset it to its default value |
| 5519 | * before starting auto-detection. |
| 5520 | */ |
| 5521 | chip->options &= ~NAND_BUSWIDTH_16; |
| 5522 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 5523 | for (; type->name != NULL; type++) { |
| 5524 | if (is_full_id_nand(type)) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5525 | if (find_full_id_nand(chip, type)) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 5526 | goto ident_done; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 5527 | } else if (dev_id == type->dev_id) { |
Brian Norris | db5b09f | 2015-05-22 10:43:12 -0700 | [diff] [blame] | 5528 | break; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 5529 | } |
| 5530 | } |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 5531 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 5532 | chip->onfi_version = 0; |
| 5533 | if (!type->name || !type->pagesize) { |
Masahiro Yamada | 35fc519 | 2014-04-09 16:26:26 +0900 | [diff] [blame] | 5534 | /* Check if the chip is ONFI compliant */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5535 | if (nand_flash_detect_onfi(chip)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5536 | goto ident_done; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5537 | |
| 5538 | /* Check if the chip is JEDEC compliant */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5539 | if (nand_flash_detect_jedec(chip)) |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5540 | goto ident_done; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 5541 | } |
| 5542 | |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 5543 | if (!type->name) |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 5544 | return -ENODEV; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5545 | |
Thomas Gleixner | ba0251f | 2006-05-27 01:02:13 +0200 | [diff] [blame] | 5546 | if (!mtd->name) |
| 5547 | mtd->name = type->name; |
| 5548 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 5549 | chip->chipsize = (uint64_t)type->chipsize << 20; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5550 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5551 | if (!type->pagesize) |
| 5552 | nand_manufacturer_detect(chip); |
| 5553 | else |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5554 | nand_decode_id(chip, type); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5555 | |
Brian Norris | bf7a01b | 2012-07-13 09:28:24 -0700 | [diff] [blame] | 5556 | /* Get chip options */ |
| 5557 | chip->options |= type->options; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 5558 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 5559 | ident_done: |
| 5560 | |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 5561 | if (chip->options & NAND_BUSWIDTH_AUTO) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5562 | WARN_ON(busw & NAND_BUSWIDTH_16); |
| 5563 | nand_set_defaults(chip); |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 5564 | } else if (busw != (chip->options & NAND_BUSWIDTH_16)) { |
| 5565 | /* |
| 5566 | * Check, if buswidth is correct. Hardware drivers should set |
| 5567 | * chip correct! |
| 5568 | */ |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 5569 | 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] | 5570 | maf_id, dev_id); |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 5571 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 5572 | mtd->name); |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5573 | pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8, |
| 5574 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 5575 | return -EINVAL; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5576 | } |
| 5577 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 5578 | nand_decode_bbm_options(chip); |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 5579 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5580 | /* Calculate the address shift from the page size */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5581 | chip->page_shift = ffs(mtd->writesize) - 1; |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5582 | /* Convert chipsize to number of pages per chip -1 */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5583 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5584 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5585 | chip->bbt_erase_shift = chip->phys_erase_shift = |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5586 | ffs(mtd->erasesize) - 1; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 5587 | if (chip->chipsize & 0xffffffff) |
| 5588 | chip->chip_shift = ffs((unsigned)chip->chipsize) - 1; |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 5589 | else { |
| 5590 | chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)); |
| 5591 | chip->chip_shift += 32 - 1; |
| 5592 | } |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5593 | |
Masahiro Yamada | 14157f8 | 2017-09-13 11:05:50 +0900 | [diff] [blame] | 5594 | if (chip->chip_shift - chip->page_shift > 16) |
| 5595 | chip->options |= NAND_ROW_ADDR_3; |
| 5596 | |
Artem Bityutskiy | 26d9be1 | 2011-04-28 20:26:59 +0300 | [diff] [blame] | 5597 | chip->badblockbits = 8; |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 5598 | chip->erase = single_erase; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5599 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5600 | /* Do not replace user supplied command function! */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5601 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 5602 | chip->cmdfunc = nand_command_lp; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5603 | |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 5604 | 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] | 5605 | maf_id, dev_id); |
Huang Shijie | ffdac6cd | 2014-02-21 13:39:41 +0800 | [diff] [blame] | 5606 | |
| 5607 | if (chip->onfi_version) |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 5608 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 5609 | chip->onfi_params.model); |
Huang Shijie | ffdac6cd | 2014-02-21 13:39:41 +0800 | [diff] [blame] | 5610 | else if (chip->jedec_version) |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 5611 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 5612 | chip->jedec_params.model); |
Huang Shijie | ffdac6cd | 2014-02-21 13:39:41 +0800 | [diff] [blame] | 5613 | else |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 5614 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 5615 | type->name); |
Huang Shijie | ffdac6cd | 2014-02-21 13:39:41 +0800 | [diff] [blame] | 5616 | |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 5617 | 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] | 5618 | (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC", |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 5619 | mtd->erasesize >> 10, mtd->writesize, mtd->oobsize); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 5620 | return 0; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5621 | } |
| 5622 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 5623 | static const char * const nand_ecc_modes[] = { |
| 5624 | [NAND_ECC_NONE] = "none", |
| 5625 | [NAND_ECC_SOFT] = "soft", |
| 5626 | [NAND_ECC_HW] = "hw", |
| 5627 | [NAND_ECC_HW_SYNDROME] = "hw_syndrome", |
| 5628 | [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first", |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 5629 | [NAND_ECC_ON_DIE] = "on-die", |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 5630 | }; |
| 5631 | |
| 5632 | static int of_get_nand_ecc_mode(struct device_node *np) |
| 5633 | { |
| 5634 | const char *pm; |
| 5635 | int err, i; |
| 5636 | |
| 5637 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 5638 | if (err < 0) |
| 5639 | return err; |
| 5640 | |
| 5641 | for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++) |
| 5642 | if (!strcasecmp(pm, nand_ecc_modes[i])) |
| 5643 | return i; |
| 5644 | |
Rafał Miłecki | ae211bc | 2016-04-17 22:53:06 +0200 | [diff] [blame] | 5645 | /* |
| 5646 | * For backward compatibility we support few obsoleted values that don't |
| 5647 | * have their mappings into nand_ecc_modes_t anymore (they were merged |
| 5648 | * with other enums). |
| 5649 | */ |
| 5650 | if (!strcasecmp(pm, "soft_bch")) |
| 5651 | return NAND_ECC_SOFT; |
| 5652 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 5653 | return -ENODEV; |
| 5654 | } |
| 5655 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 5656 | static const char * const nand_ecc_algos[] = { |
| 5657 | [NAND_ECC_HAMMING] = "hamming", |
| 5658 | [NAND_ECC_BCH] = "bch", |
| 5659 | }; |
| 5660 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 5661 | static int of_get_nand_ecc_algo(struct device_node *np) |
| 5662 | { |
| 5663 | const char *pm; |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 5664 | int err, i; |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 5665 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 5666 | err = of_property_read_string(np, "nand-ecc-algo", &pm); |
| 5667 | if (!err) { |
| 5668 | for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++) |
| 5669 | if (!strcasecmp(pm, nand_ecc_algos[i])) |
| 5670 | return i; |
| 5671 | return -ENODEV; |
| 5672 | } |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 5673 | |
| 5674 | /* |
| 5675 | * For backward compatibility we also read "nand-ecc-mode" checking |
| 5676 | * for some obsoleted values that were specifying ECC algorithm. |
| 5677 | */ |
| 5678 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 5679 | if (err < 0) |
| 5680 | return err; |
| 5681 | |
| 5682 | if (!strcasecmp(pm, "soft")) |
| 5683 | return NAND_ECC_HAMMING; |
| 5684 | else if (!strcasecmp(pm, "soft_bch")) |
| 5685 | return NAND_ECC_BCH; |
| 5686 | |
| 5687 | return -ENODEV; |
| 5688 | } |
| 5689 | |
| 5690 | static int of_get_nand_ecc_step_size(struct device_node *np) |
| 5691 | { |
| 5692 | int ret; |
| 5693 | u32 val; |
| 5694 | |
| 5695 | ret = of_property_read_u32(np, "nand-ecc-step-size", &val); |
| 5696 | return ret ? ret : val; |
| 5697 | } |
| 5698 | |
| 5699 | static int of_get_nand_ecc_strength(struct device_node *np) |
| 5700 | { |
| 5701 | int ret; |
| 5702 | u32 val; |
| 5703 | |
| 5704 | ret = of_property_read_u32(np, "nand-ecc-strength", &val); |
| 5705 | return ret ? ret : val; |
| 5706 | } |
| 5707 | |
| 5708 | static int of_get_nand_bus_width(struct device_node *np) |
| 5709 | { |
| 5710 | u32 val; |
| 5711 | |
| 5712 | if (of_property_read_u32(np, "nand-bus-width", &val)) |
| 5713 | return 8; |
| 5714 | |
| 5715 | switch (val) { |
| 5716 | case 8: |
| 5717 | case 16: |
| 5718 | return val; |
| 5719 | default: |
| 5720 | return -EIO; |
| 5721 | } |
| 5722 | } |
| 5723 | |
| 5724 | static bool of_get_nand_on_flash_bbt(struct device_node *np) |
| 5725 | { |
| 5726 | return of_property_read_bool(np, "nand-on-flash-bbt"); |
| 5727 | } |
| 5728 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 5729 | static int nand_dt_init(struct nand_chip *chip) |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 5730 | { |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 5731 | struct device_node *dn = nand_get_flash_node(chip); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 5732 | int ecc_mode, ecc_algo, ecc_strength, ecc_step; |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 5733 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 5734 | if (!dn) |
| 5735 | return 0; |
| 5736 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 5737 | if (of_get_nand_bus_width(dn) == 16) |
| 5738 | chip->options |= NAND_BUSWIDTH_16; |
| 5739 | |
| 5740 | if (of_get_nand_on_flash_bbt(dn)) |
| 5741 | chip->bbt_options |= NAND_BBT_USE_FLASH; |
| 5742 | |
| 5743 | ecc_mode = of_get_nand_ecc_mode(dn); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 5744 | ecc_algo = of_get_nand_ecc_algo(dn); |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 5745 | ecc_strength = of_get_nand_ecc_strength(dn); |
| 5746 | ecc_step = of_get_nand_ecc_step_size(dn); |
| 5747 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 5748 | if (ecc_mode >= 0) |
| 5749 | chip->ecc.mode = ecc_mode; |
| 5750 | |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 5751 | if (ecc_algo >= 0) |
| 5752 | chip->ecc.algo = ecc_algo; |
| 5753 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 5754 | if (ecc_strength >= 0) |
| 5755 | chip->ecc.strength = ecc_strength; |
| 5756 | |
| 5757 | if (ecc_step > 0) |
| 5758 | chip->ecc.size = ecc_step; |
| 5759 | |
Boris Brezillon | ba78ee0 | 2016-06-08 17:04:22 +0200 | [diff] [blame] | 5760 | if (of_property_read_bool(dn, "nand-ecc-maximize")) |
| 5761 | chip->ecc.options |= NAND_ECC_MAXIMIZE; |
| 5762 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 5763 | return 0; |
| 5764 | } |
| 5765 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5766 | /** |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5767 | * nand_scan_ident - [NAND Interface] Scan for the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5768 | * @mtd: MTD device structure |
| 5769 | * @maxchips: number of chips to scan for |
| 5770 | * @table: alternative NAND ID table |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5771 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5772 | * This is the first phase of the normal nand_scan() function. It reads the |
| 5773 | * flash ID and sets up MTD fields accordingly. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5774 | * |
| 5775 | */ |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 5776 | int nand_scan_ident(struct mtd_info *mtd, int maxchips, |
| 5777 | struct nand_flash_dev *table) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5778 | { |
Cai Zhiyong | bb77082 | 2013-12-25 20:11:15 +0800 | [diff] [blame] | 5779 | int i, nand_maf_id, nand_dev_id; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 5780 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 5781 | int ret; |
| 5782 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 5783 | /* Enforce the right timings for reset/detection */ |
| 5784 | onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0); |
| 5785 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 5786 | ret = nand_dt_init(chip); |
| 5787 | if (ret) |
| 5788 | return ret; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5789 | |
Brian Norris | f7a8e38 | 2016-01-05 10:39:45 -0800 | [diff] [blame] | 5790 | if (!mtd->name && mtd->dev.parent) |
| 5791 | mtd->name = dev_name(mtd->dev.parent); |
| 5792 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 5793 | /* |
| 5794 | * ->cmdfunc() is legacy and will only be used if ->exec_op() is not |
| 5795 | * populated. |
| 5796 | */ |
| 5797 | if (!chip->exec_op) { |
Andrey Smirnov | 76fe334 | 2016-07-21 14:59:20 -0700 | [diff] [blame] | 5798 | /* |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 5799 | * Default functions assigned for ->cmdfunc() and |
| 5800 | * ->select_chip() both expect ->cmd_ctrl() to be populated. |
Andrey Smirnov | 76fe334 | 2016-07-21 14:59:20 -0700 | [diff] [blame] | 5801 | */ |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 5802 | if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) { |
| 5803 | pr_err("->cmd_ctrl() should be provided\n"); |
| 5804 | return -EINVAL; |
| 5805 | } |
Andrey Smirnov | 76fe334 | 2016-07-21 14:59:20 -0700 | [diff] [blame] | 5806 | } |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 5807 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5808 | /* Set the default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5809 | nand_set_defaults(chip); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5810 | |
| 5811 | /* Read the flash type */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame] | 5812 | ret = nand_detect(chip, table); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 5813 | if (ret) { |
Ben Dooks | b1c6e6d | 2009-11-02 18:12:33 +0000 | [diff] [blame] | 5814 | if (!(chip->options & NAND_SCAN_SILENT_NODEV)) |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 5815 | pr_warn("No NAND device found\n"); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5816 | chip->select_chip(mtd, -1); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 5817 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5818 | } |
| 5819 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 5820 | nand_maf_id = chip->id.data[0]; |
| 5821 | nand_dev_id = chip->id.data[1]; |
| 5822 | |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 5823 | chip->select_chip(mtd, -1); |
| 5824 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5825 | /* Check for a chip array */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5826 | for (i = 1; i < maxchips; i++) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5827 | u8 id[2]; |
| 5828 | |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 5829 | /* See comment in nand_get_flash_type for reset */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 5830 | nand_reset(chip, i); |
| 5831 | |
| 5832 | chip->select_chip(mtd, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5833 | /* Send the command for reading device ID */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5834 | nand_readid_op(chip, 0, id, sizeof(id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5835 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5836 | if (nand_maf_id != id[0] || nand_dev_id != id[1]) { |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 5837 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5838 | break; |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 5839 | } |
| 5840 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5841 | } |
| 5842 | if (i > 1) |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 5843 | pr_info("%d chips detected\n", i); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5844 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5845 | /* Store the number of chips and calc total size for mtd */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5846 | chip->numchips = i; |
| 5847 | mtd->size = i * chip->chipsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5848 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5849 | return 0; |
| 5850 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 5851 | EXPORT_SYMBOL(nand_scan_ident); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5852 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5853 | static int nand_set_ecc_soft_ops(struct mtd_info *mtd) |
| 5854 | { |
| 5855 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 5856 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 5857 | |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 5858 | if (WARN_ON(ecc->mode != NAND_ECC_SOFT)) |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5859 | return -EINVAL; |
| 5860 | |
| 5861 | switch (ecc->algo) { |
| 5862 | case NAND_ECC_HAMMING: |
| 5863 | ecc->calculate = nand_calculate_ecc; |
| 5864 | ecc->correct = nand_correct_data; |
| 5865 | ecc->read_page = nand_read_page_swecc; |
| 5866 | ecc->read_subpage = nand_read_subpage; |
| 5867 | ecc->write_page = nand_write_page_swecc; |
| 5868 | ecc->read_page_raw = nand_read_page_raw; |
| 5869 | ecc->write_page_raw = nand_write_page_raw; |
| 5870 | ecc->read_oob = nand_read_oob_std; |
| 5871 | ecc->write_oob = nand_write_oob_std; |
| 5872 | if (!ecc->size) |
| 5873 | ecc->size = 256; |
| 5874 | ecc->bytes = 3; |
| 5875 | ecc->strength = 1; |
| 5876 | return 0; |
| 5877 | case NAND_ECC_BCH: |
| 5878 | if (!mtd_nand_has_bch()) { |
| 5879 | WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n"); |
| 5880 | return -EINVAL; |
| 5881 | } |
| 5882 | ecc->calculate = nand_bch_calculate_ecc; |
| 5883 | ecc->correct = nand_bch_correct_data; |
| 5884 | ecc->read_page = nand_read_page_swecc; |
| 5885 | ecc->read_subpage = nand_read_subpage; |
| 5886 | ecc->write_page = nand_write_page_swecc; |
| 5887 | ecc->read_page_raw = nand_read_page_raw; |
| 5888 | ecc->write_page_raw = nand_write_page_raw; |
| 5889 | ecc->read_oob = nand_read_oob_std; |
| 5890 | ecc->write_oob = nand_write_oob_std; |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 5891 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5892 | /* |
| 5893 | * Board driver should supply ecc.size and ecc.strength |
| 5894 | * values to select how many bits are correctable. |
| 5895 | * Otherwise, default to 4 bits for large page devices. |
| 5896 | */ |
| 5897 | if (!ecc->size && (mtd->oobsize >= 64)) { |
| 5898 | ecc->size = 512; |
| 5899 | ecc->strength = 4; |
| 5900 | } |
| 5901 | |
| 5902 | /* |
| 5903 | * if no ecc placement scheme was provided pickup the default |
| 5904 | * large page one. |
| 5905 | */ |
| 5906 | if (!mtd->ooblayout) { |
| 5907 | /* handle large page devices only */ |
| 5908 | if (mtd->oobsize < 64) { |
| 5909 | WARN(1, "OOB layout is required when using software BCH on small pages\n"); |
| 5910 | return -EINVAL; |
| 5911 | } |
| 5912 | |
| 5913 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops); |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 5914 | |
| 5915 | } |
| 5916 | |
| 5917 | /* |
| 5918 | * We can only maximize ECC config when the default layout is |
| 5919 | * used, otherwise we don't know how many bytes can really be |
| 5920 | * used. |
| 5921 | */ |
| 5922 | if (mtd->ooblayout == &nand_ooblayout_lp_ops && |
| 5923 | ecc->options & NAND_ECC_MAXIMIZE) { |
| 5924 | int steps, bytes; |
| 5925 | |
| 5926 | /* Always prefer 1k blocks over 512bytes ones */ |
| 5927 | ecc->size = 1024; |
| 5928 | steps = mtd->writesize / ecc->size; |
| 5929 | |
| 5930 | /* Reserve 2 bytes for the BBM */ |
| 5931 | bytes = (mtd->oobsize - 2) / steps; |
| 5932 | ecc->strength = bytes * 8 / fls(8 * ecc->size); |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5933 | } |
| 5934 | |
| 5935 | /* See nand_bch_init() for details. */ |
| 5936 | ecc->bytes = 0; |
| 5937 | ecc->priv = nand_bch_init(mtd); |
| 5938 | if (!ecc->priv) { |
| 5939 | WARN(1, "BCH ECC initialization failed!\n"); |
| 5940 | return -EINVAL; |
| 5941 | } |
| 5942 | return 0; |
| 5943 | default: |
| 5944 | WARN(1, "Unsupported ECC algorithm!\n"); |
| 5945 | return -EINVAL; |
| 5946 | } |
| 5947 | } |
| 5948 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 5949 | /** |
| 5950 | * nand_check_ecc_caps - check the sanity of preset ECC settings |
| 5951 | * @chip: nand chip info structure |
| 5952 | * @caps: ECC caps info structure |
| 5953 | * @oobavail: OOB size that the ECC engine can use |
| 5954 | * |
| 5955 | * When ECC step size and strength are already set, check if they are supported |
| 5956 | * by the controller and the calculated ECC bytes fit within the chip's OOB. |
| 5957 | * On success, the calculated ECC bytes is set. |
| 5958 | */ |
| 5959 | int nand_check_ecc_caps(struct nand_chip *chip, |
| 5960 | const struct nand_ecc_caps *caps, int oobavail) |
| 5961 | { |
| 5962 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 5963 | const struct nand_ecc_step_info *stepinfo; |
| 5964 | int preset_step = chip->ecc.size; |
| 5965 | int preset_strength = chip->ecc.strength; |
| 5966 | int nsteps, ecc_bytes; |
| 5967 | int i, j; |
| 5968 | |
| 5969 | if (WARN_ON(oobavail < 0)) |
| 5970 | return -EINVAL; |
| 5971 | |
| 5972 | if (!preset_step || !preset_strength) |
| 5973 | return -ENODATA; |
| 5974 | |
| 5975 | nsteps = mtd->writesize / preset_step; |
| 5976 | |
| 5977 | for (i = 0; i < caps->nstepinfos; i++) { |
| 5978 | stepinfo = &caps->stepinfos[i]; |
| 5979 | |
| 5980 | if (stepinfo->stepsize != preset_step) |
| 5981 | continue; |
| 5982 | |
| 5983 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 5984 | if (stepinfo->strengths[j] != preset_strength) |
| 5985 | continue; |
| 5986 | |
| 5987 | ecc_bytes = caps->calc_ecc_bytes(preset_step, |
| 5988 | preset_strength); |
| 5989 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 5990 | return ecc_bytes; |
| 5991 | |
| 5992 | if (ecc_bytes * nsteps > oobavail) { |
| 5993 | pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB", |
| 5994 | preset_step, preset_strength); |
| 5995 | return -ENOSPC; |
| 5996 | } |
| 5997 | |
| 5998 | chip->ecc.bytes = ecc_bytes; |
| 5999 | |
| 6000 | return 0; |
| 6001 | } |
| 6002 | } |
| 6003 | |
| 6004 | pr_err("ECC (step, strength) = (%d, %d) not supported on this controller", |
| 6005 | preset_step, preset_strength); |
| 6006 | |
| 6007 | return -ENOTSUPP; |
| 6008 | } |
| 6009 | EXPORT_SYMBOL_GPL(nand_check_ecc_caps); |
| 6010 | |
| 6011 | /** |
| 6012 | * nand_match_ecc_req - meet the chip's requirement with least ECC bytes |
| 6013 | * @chip: nand chip info structure |
| 6014 | * @caps: ECC engine caps info structure |
| 6015 | * @oobavail: OOB size that the ECC engine can use |
| 6016 | * |
| 6017 | * If a chip's ECC requirement is provided, try to meet it with the least |
| 6018 | * number of ECC bytes (i.e. with the largest number of OOB-free bytes). |
| 6019 | * On success, the chosen ECC settings are set. |
| 6020 | */ |
| 6021 | int nand_match_ecc_req(struct nand_chip *chip, |
| 6022 | const struct nand_ecc_caps *caps, int oobavail) |
| 6023 | { |
| 6024 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 6025 | const struct nand_ecc_step_info *stepinfo; |
| 6026 | int req_step = chip->ecc_step_ds; |
| 6027 | int req_strength = chip->ecc_strength_ds; |
| 6028 | int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total; |
| 6029 | int best_step, best_strength, best_ecc_bytes; |
| 6030 | int best_ecc_bytes_total = INT_MAX; |
| 6031 | int i, j; |
| 6032 | |
| 6033 | if (WARN_ON(oobavail < 0)) |
| 6034 | return -EINVAL; |
| 6035 | |
| 6036 | /* No information provided by the NAND chip */ |
| 6037 | if (!req_step || !req_strength) |
| 6038 | return -ENOTSUPP; |
| 6039 | |
| 6040 | /* number of correctable bits the chip requires in a page */ |
| 6041 | req_corr = mtd->writesize / req_step * req_strength; |
| 6042 | |
| 6043 | for (i = 0; i < caps->nstepinfos; i++) { |
| 6044 | stepinfo = &caps->stepinfos[i]; |
| 6045 | step_size = stepinfo->stepsize; |
| 6046 | |
| 6047 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 6048 | strength = stepinfo->strengths[j]; |
| 6049 | |
| 6050 | /* |
| 6051 | * If both step size and strength are smaller than the |
| 6052 | * chip's requirement, it is not easy to compare the |
| 6053 | * resulted reliability. |
| 6054 | */ |
| 6055 | if (step_size < req_step && strength < req_strength) |
| 6056 | continue; |
| 6057 | |
| 6058 | if (mtd->writesize % step_size) |
| 6059 | continue; |
| 6060 | |
| 6061 | nsteps = mtd->writesize / step_size; |
| 6062 | |
| 6063 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 6064 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 6065 | continue; |
| 6066 | ecc_bytes_total = ecc_bytes * nsteps; |
| 6067 | |
| 6068 | if (ecc_bytes_total > oobavail || |
| 6069 | strength * nsteps < req_corr) |
| 6070 | continue; |
| 6071 | |
| 6072 | /* |
| 6073 | * We assume the best is to meet the chip's requrement |
| 6074 | * with the least number of ECC bytes. |
| 6075 | */ |
| 6076 | if (ecc_bytes_total < best_ecc_bytes_total) { |
| 6077 | best_ecc_bytes_total = ecc_bytes_total; |
| 6078 | best_step = step_size; |
| 6079 | best_strength = strength; |
| 6080 | best_ecc_bytes = ecc_bytes; |
| 6081 | } |
| 6082 | } |
| 6083 | } |
| 6084 | |
| 6085 | if (best_ecc_bytes_total == INT_MAX) |
| 6086 | return -ENOTSUPP; |
| 6087 | |
| 6088 | chip->ecc.size = best_step; |
| 6089 | chip->ecc.strength = best_strength; |
| 6090 | chip->ecc.bytes = best_ecc_bytes; |
| 6091 | |
| 6092 | return 0; |
| 6093 | } |
| 6094 | EXPORT_SYMBOL_GPL(nand_match_ecc_req); |
| 6095 | |
| 6096 | /** |
| 6097 | * nand_maximize_ecc - choose the max ECC strength available |
| 6098 | * @chip: nand chip info structure |
| 6099 | * @caps: ECC engine caps info structure |
| 6100 | * @oobavail: OOB size that the ECC engine can use |
| 6101 | * |
| 6102 | * Choose the max ECC strength that is supported on the controller, and can fit |
| 6103 | * within the chip's OOB. On success, the chosen ECC settings are set. |
| 6104 | */ |
| 6105 | int nand_maximize_ecc(struct nand_chip *chip, |
| 6106 | const struct nand_ecc_caps *caps, int oobavail) |
| 6107 | { |
| 6108 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 6109 | const struct nand_ecc_step_info *stepinfo; |
| 6110 | int step_size, strength, nsteps, ecc_bytes, corr; |
| 6111 | int best_corr = 0; |
| 6112 | int best_step = 0; |
| 6113 | int best_strength, best_ecc_bytes; |
| 6114 | int i, j; |
| 6115 | |
| 6116 | if (WARN_ON(oobavail < 0)) |
| 6117 | return -EINVAL; |
| 6118 | |
| 6119 | for (i = 0; i < caps->nstepinfos; i++) { |
| 6120 | stepinfo = &caps->stepinfos[i]; |
| 6121 | step_size = stepinfo->stepsize; |
| 6122 | |
| 6123 | /* If chip->ecc.size is already set, respect it */ |
| 6124 | if (chip->ecc.size && step_size != chip->ecc.size) |
| 6125 | continue; |
| 6126 | |
| 6127 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 6128 | strength = stepinfo->strengths[j]; |
| 6129 | |
| 6130 | if (mtd->writesize % step_size) |
| 6131 | continue; |
| 6132 | |
| 6133 | nsteps = mtd->writesize / step_size; |
| 6134 | |
| 6135 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 6136 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 6137 | continue; |
| 6138 | |
| 6139 | if (ecc_bytes * nsteps > oobavail) |
| 6140 | continue; |
| 6141 | |
| 6142 | corr = strength * nsteps; |
| 6143 | |
| 6144 | /* |
| 6145 | * If the number of correctable bits is the same, |
| 6146 | * bigger step_size has more reliability. |
| 6147 | */ |
| 6148 | if (corr > best_corr || |
| 6149 | (corr == best_corr && step_size > best_step)) { |
| 6150 | best_corr = corr; |
| 6151 | best_step = step_size; |
| 6152 | best_strength = strength; |
| 6153 | best_ecc_bytes = ecc_bytes; |
| 6154 | } |
| 6155 | } |
| 6156 | } |
| 6157 | |
| 6158 | if (!best_corr) |
| 6159 | return -ENOTSUPP; |
| 6160 | |
| 6161 | chip->ecc.size = best_step; |
| 6162 | chip->ecc.strength = best_strength; |
| 6163 | chip->ecc.bytes = best_ecc_bytes; |
| 6164 | |
| 6165 | return 0; |
| 6166 | } |
| 6167 | EXPORT_SYMBOL_GPL(nand_maximize_ecc); |
| 6168 | |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 6169 | /* |
| 6170 | * Check if the chip configuration meet the datasheet requirements. |
| 6171 | |
| 6172 | * If our configuration corrects A bits per B bytes and the minimum |
| 6173 | * required correction level is X bits per Y bytes, then we must ensure |
| 6174 | * both of the following are true: |
| 6175 | * |
| 6176 | * (1) A / B >= X / Y |
| 6177 | * (2) A >= X |
| 6178 | * |
| 6179 | * Requirement (1) ensures we can correct for the required bitflip density. |
| 6180 | * Requirement (2) ensures we can correct even when all bitflips are clumped |
| 6181 | * in the same sector. |
| 6182 | */ |
| 6183 | static bool nand_ecc_strength_good(struct mtd_info *mtd) |
| 6184 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 6185 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 6186 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 6187 | int corr, ds_corr; |
| 6188 | |
| 6189 | if (ecc->size == 0 || chip->ecc_step_ds == 0) |
| 6190 | /* Not enough information */ |
| 6191 | return true; |
| 6192 | |
| 6193 | /* |
| 6194 | * We get the number of corrected bits per page to compare |
| 6195 | * the correction density. |
| 6196 | */ |
| 6197 | corr = (mtd->writesize * ecc->strength) / ecc->size; |
| 6198 | ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds; |
| 6199 | |
| 6200 | return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds; |
| 6201 | } |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 6202 | |
| 6203 | /** |
| 6204 | * nand_scan_tail - [NAND Interface] Scan for the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 6205 | * @mtd: MTD device structure |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 6206 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 6207 | * This is the second phase of the normal nand_scan() function. It fills out |
| 6208 | * all the uninitialized function pointers with the defaults and scans for a |
| 6209 | * bad block table if appropriate. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 6210 | */ |
| 6211 | int nand_scan_tail(struct mtd_info *mtd) |
| 6212 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 6213 | struct nand_chip *chip = mtd_to_nand(mtd); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6214 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6215 | int ret, i; |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 6216 | |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 6217 | /* 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] | 6218 | if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) && |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 6219 | !(chip->bbt_options & NAND_BBT_USE_FLASH))) { |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6220 | return -EINVAL; |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 6221 | } |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 6222 | |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 6223 | chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL); |
Boris Brezillon | aeb93af | 2017-12-05 12:09:29 +0100 | [diff] [blame] | 6224 | if (!chip->data_buf) |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6225 | return -ENOMEM; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 6226 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6227 | /* |
| 6228 | * FIXME: some NAND manufacturer drivers expect the first die to be |
| 6229 | * selected when manufacturer->init() is called. They should be fixed |
| 6230 | * to explictly select the relevant die when interacting with the NAND |
| 6231 | * chip. |
| 6232 | */ |
| 6233 | chip->select_chip(mtd, 0); |
| 6234 | ret = nand_manufacturer_init(chip); |
| 6235 | chip->select_chip(mtd, -1); |
| 6236 | if (ret) |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 6237 | goto err_free_buf; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6238 | |
David Woodhouse | 7dcdcbef | 2006-10-21 17:09:53 +0100 | [diff] [blame] | 6239 | /* Set the internal oob buffer location, just after the page data */ |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 6240 | chip->oob_poi = chip->data_buf + mtd->writesize; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 6241 | |
| 6242 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 6243 | * If no default placement scheme is given, select an appropriate one. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 6244 | */ |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 6245 | if (!mtd->ooblayout && |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 6246 | !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 6247 | switch (mtd->oobsize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6248 | case 8: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6249 | case 16: |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 6250 | mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6251 | break; |
| 6252 | case 64: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 6253 | case 128: |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 6254 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops); |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 6255 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6256 | default: |
Miquel Raynal | 882fd15 | 2017-08-26 17:19:15 +0200 | [diff] [blame] | 6257 | /* |
| 6258 | * Expose the whole OOB area to users if ECC_NONE |
| 6259 | * is passed. We could do that for all kind of |
| 6260 | * ->oobsize, but we must keep the old large/small |
| 6261 | * page with ECC layout when ->oobsize <= 128 for |
| 6262 | * compatibility reasons. |
| 6263 | */ |
| 6264 | if (ecc->mode == NAND_ECC_NONE) { |
| 6265 | mtd_set_ooblayout(mtd, |
| 6266 | &nand_ooblayout_lp_ops); |
| 6267 | break; |
| 6268 | } |
| 6269 | |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 6270 | WARN(1, "No oob scheme defined for oobsize %d\n", |
| 6271 | mtd->oobsize); |
| 6272 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6273 | goto err_nand_manuf_cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6274 | } |
| 6275 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 6276 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 6277 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 6278 | * Check ECC mode, default to software if 3byte/512byte hardware ECC is |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 6279 | * selected and we have 256 byte pagesize fallback to software ECC |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 6280 | */ |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 6281 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6282 | switch (ecc->mode) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 6283 | case NAND_ECC_HW_OOB_FIRST: |
| 6284 | /* Similar to NAND_ECC_HW, but a separate read_page handle */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6285 | if (!ecc->calculate || !ecc->correct || !ecc->hwctl) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 6286 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 6287 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6288 | goto err_nand_manuf_cleanup; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 6289 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6290 | if (!ecc->read_page) |
| 6291 | ecc->read_page = nand_read_page_hwecc_oob_first; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 6292 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 6293 | case NAND_ECC_HW: |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 6294 | /* Use standard hwecc read page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6295 | if (!ecc->read_page) |
| 6296 | ecc->read_page = nand_read_page_hwecc; |
| 6297 | if (!ecc->write_page) |
| 6298 | ecc->write_page = nand_write_page_hwecc; |
| 6299 | if (!ecc->read_page_raw) |
| 6300 | ecc->read_page_raw = nand_read_page_raw; |
| 6301 | if (!ecc->write_page_raw) |
| 6302 | ecc->write_page_raw = nand_write_page_raw; |
| 6303 | if (!ecc->read_oob) |
| 6304 | ecc->read_oob = nand_read_oob_std; |
| 6305 | if (!ecc->write_oob) |
| 6306 | ecc->write_oob = nand_write_oob_std; |
| 6307 | if (!ecc->read_subpage) |
| 6308 | ecc->read_subpage = nand_read_subpage; |
Helmut Schaa | 44991b3 | 2014-04-09 11:13:24 +0200 | [diff] [blame] | 6309 | if (!ecc->write_subpage && ecc->hwctl && ecc->calculate) |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6310 | ecc->write_subpage = nand_write_subpage_hwecc; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 6311 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 6312 | case NAND_ECC_HW_SYNDROME: |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6313 | if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) && |
| 6314 | (!ecc->read_page || |
| 6315 | ecc->read_page == nand_read_page_hwecc || |
| 6316 | !ecc->write_page || |
| 6317 | ecc->write_page == nand_write_page_hwecc)) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 6318 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 6319 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6320 | goto err_nand_manuf_cleanup; |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 6321 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 6322 | /* Use standard syndrome read/write page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6323 | if (!ecc->read_page) |
| 6324 | ecc->read_page = nand_read_page_syndrome; |
| 6325 | if (!ecc->write_page) |
| 6326 | ecc->write_page = nand_write_page_syndrome; |
| 6327 | if (!ecc->read_page_raw) |
| 6328 | ecc->read_page_raw = nand_read_page_raw_syndrome; |
| 6329 | if (!ecc->write_page_raw) |
| 6330 | ecc->write_page_raw = nand_write_page_raw_syndrome; |
| 6331 | if (!ecc->read_oob) |
| 6332 | ecc->read_oob = nand_read_oob_syndrome; |
| 6333 | if (!ecc->write_oob) |
| 6334 | ecc->write_oob = nand_write_oob_syndrome; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 6335 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6336 | if (mtd->writesize >= ecc->size) { |
| 6337 | if (!ecc->strength) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 6338 | WARN(1, "Driver must set ecc.strength when using hardware ECC\n"); |
| 6339 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6340 | goto err_nand_manuf_cleanup; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 6341 | } |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 6342 | break; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 6343 | } |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 6344 | pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n", |
| 6345 | ecc->size, mtd->writesize); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6346 | ecc->mode = NAND_ECC_SOFT; |
Rafał Miłecki | e9d4fae | 2016-04-17 22:53:02 +0200 | [diff] [blame] | 6347 | ecc->algo = NAND_ECC_HAMMING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6348 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 6349 | case NAND_ECC_SOFT: |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 6350 | ret = nand_set_ecc_soft_ops(mtd); |
| 6351 | if (ret) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 6352 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6353 | goto err_nand_manuf_cleanup; |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 6354 | } |
| 6355 | break; |
| 6356 | |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 6357 | case NAND_ECC_ON_DIE: |
| 6358 | if (!ecc->read_page || !ecc->write_page) { |
| 6359 | WARN(1, "No ECC functions supplied; on-die ECC not possible\n"); |
| 6360 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6361 | goto err_nand_manuf_cleanup; |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 6362 | } |
| 6363 | if (!ecc->read_oob) |
| 6364 | ecc->read_oob = nand_read_oob_std; |
| 6365 | if (!ecc->write_oob) |
| 6366 | ecc->write_oob = nand_write_oob_std; |
| 6367 | break; |
| 6368 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 6369 | case NAND_ECC_NONE: |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 6370 | 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] | 6371 | ecc->read_page = nand_read_page_raw; |
| 6372 | ecc->write_page = nand_write_page_raw; |
| 6373 | ecc->read_oob = nand_read_oob_std; |
| 6374 | ecc->read_page_raw = nand_read_page_raw; |
| 6375 | ecc->write_page_raw = nand_write_page_raw; |
| 6376 | ecc->write_oob = nand_write_oob_std; |
| 6377 | ecc->size = mtd->writesize; |
| 6378 | ecc->bytes = 0; |
| 6379 | ecc->strength = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6380 | break; |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 6381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6382 | default: |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 6383 | WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode); |
| 6384 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6385 | goto err_nand_manuf_cleanup; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 6386 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6387 | |
Boris Brezillon | aeb93af | 2017-12-05 12:09:29 +0100 | [diff] [blame] | 6388 | if (ecc->correct || ecc->calculate) { |
| 6389 | ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL); |
| 6390 | ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL); |
| 6391 | if (!ecc->calc_buf || !ecc->code_buf) { |
| 6392 | ret = -ENOMEM; |
| 6393 | goto err_nand_manuf_cleanup; |
| 6394 | } |
| 6395 | } |
| 6396 | |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 6397 | /* For many systems, the standard OOB write also works for raw */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6398 | if (!ecc->read_oob_raw) |
| 6399 | ecc->read_oob_raw = ecc->read_oob; |
| 6400 | if (!ecc->write_oob_raw) |
| 6401 | ecc->write_oob_raw = ecc->write_oob; |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 6402 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 6403 | /* propagate ecc info to mtd_info */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 6404 | mtd->ecc_strength = ecc->strength; |
| 6405 | mtd->ecc_step_size = ecc->size; |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 6406 | |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 6407 | /* |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 6408 | * 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] | 6409 | * mode. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 6410 | */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6411 | ecc->steps = mtd->writesize / ecc->size; |
| 6412 | if (ecc->steps * ecc->size != mtd->writesize) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 6413 | WARN(1, "Invalid ECC parameters\n"); |
| 6414 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6415 | goto err_nand_manuf_cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6416 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6417 | ecc->total = ecc->steps * ecc->bytes; |
Masahiro Yamada | 79e0348 | 2017-05-25 13:50:20 +0900 | [diff] [blame] | 6418 | if (ecc->total > mtd->oobsize) { |
| 6419 | WARN(1, "Total number of ECC bytes exceeded oobsize\n"); |
| 6420 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6421 | goto err_nand_manuf_cleanup; |
Masahiro Yamada | 79e0348 | 2017-05-25 13:50:20 +0900 | [diff] [blame] | 6422 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 6423 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 6424 | /* |
| 6425 | * The number of bytes available for a client to place data into |
| 6426 | * the out of band area. |
| 6427 | */ |
| 6428 | ret = mtd_ooblayout_count_freebytes(mtd); |
| 6429 | if (ret < 0) |
| 6430 | ret = 0; |
| 6431 | |
| 6432 | mtd->oobavail = ret; |
| 6433 | |
| 6434 | /* ECC sanity check: warn if it's too weak */ |
| 6435 | if (!nand_ecc_strength_good(mtd)) |
| 6436 | pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n", |
| 6437 | mtd->name); |
| 6438 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 6439 | /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */ |
Huang Shijie | 1d0ed69 | 2013-09-25 14:58:10 +0800 | [diff] [blame] | 6440 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) { |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6441 | switch (ecc->steps) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 6442 | case 2: |
| 6443 | mtd->subpage_sft = 1; |
| 6444 | break; |
| 6445 | case 4: |
| 6446 | case 8: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 6447 | case 16: |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 6448 | mtd->subpage_sft = 2; |
| 6449 | break; |
| 6450 | } |
| 6451 | } |
| 6452 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; |
| 6453 | |
Thomas Gleixner | 04bbd0e | 2006-05-25 09:45:29 +0200 | [diff] [blame] | 6454 | /* Initialize state */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 6455 | chip->state = FL_READY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6456 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6457 | /* Invalidate the pagebuffer reference */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 6458 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6459 | |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 6460 | /* Large page NAND with SOFT_ECC should support subpage reads */ |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 6461 | switch (ecc->mode) { |
| 6462 | case NAND_ECC_SOFT: |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 6463 | if (chip->page_shift > 9) |
| 6464 | chip->options |= NAND_SUBPAGE_READ; |
| 6465 | break; |
| 6466 | |
| 6467 | default: |
| 6468 | break; |
| 6469 | } |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 6470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6471 | /* Fill in remaining MTD driver data */ |
Huang Shijie | 963d1c2 | 2013-09-25 14:58:21 +0800 | [diff] [blame] | 6472 | mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH; |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 6473 | mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM : |
| 6474 | MTD_CAP_NANDFLASH; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 6475 | mtd->_erase = nand_erase; |
| 6476 | mtd->_point = NULL; |
| 6477 | mtd->_unpoint = NULL; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 6478 | mtd->_panic_write = panic_nand_write; |
| 6479 | mtd->_read_oob = nand_read_oob; |
| 6480 | mtd->_write_oob = nand_write_oob; |
| 6481 | mtd->_sync = nand_sync; |
| 6482 | mtd->_lock = NULL; |
| 6483 | mtd->_unlock = NULL; |
| 6484 | mtd->_suspend = nand_suspend; |
| 6485 | mtd->_resume = nand_resume; |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 6486 | mtd->_reboot = nand_shutdown; |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 6487 | mtd->_block_isreserved = nand_block_isreserved; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 6488 | mtd->_block_isbad = nand_block_isbad; |
| 6489 | mtd->_block_markbad = nand_block_markbad; |
Zach Brown | 5671842 | 2017-01-10 13:30:20 -0600 | [diff] [blame] | 6490 | mtd->_max_bad_blocks = nand_max_bad_blocks; |
Anatolij Gustschin | cbcab65 | 2010-12-16 23:42:16 +0100 | [diff] [blame] | 6491 | mtd->writebufsize = mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6492 | |
Shmulik Ladkani | ea3b2ea | 2012-06-08 18:29:06 +0300 | [diff] [blame] | 6493 | /* |
| 6494 | * Initialize bitflip_threshold to its default prior scan_bbt() call. |
| 6495 | * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be |
| 6496 | * properly set. |
| 6497 | */ |
| 6498 | if (!mtd->bitflip_threshold) |
Brian Norris | 240181f | 2015-01-12 12:51:29 -0800 | [diff] [blame] | 6499 | mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6500 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6501 | /* Initialize the ->data_interface field. */ |
| 6502 | ret = nand_init_data_interface(chip); |
| 6503 | if (ret) |
| 6504 | goto err_nand_manuf_cleanup; |
| 6505 | |
| 6506 | /* Enter fastest possible mode on all dies. */ |
| 6507 | for (i = 0; i < chip->numchips; i++) { |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6508 | ret = nand_setup_data_interface(chip, i); |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6509 | if (ret) |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 6510 | goto err_nand_manuf_cleanup; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6511 | } |
| 6512 | |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 6513 | /* Check, if we should skip the bad block table scan */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 6514 | if (chip->options & NAND_SKIP_BBTSCAN) |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 6515 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6516 | |
| 6517 | /* Build bad block table */ |
Brian Norris | 44d4182 | 2017-05-01 17:04:50 -0700 | [diff] [blame] | 6518 | ret = chip->scan_bbt(mtd); |
| 6519 | if (ret) |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 6520 | goto err_nand_manuf_cleanup; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6521 | |
Brian Norris | 44d4182 | 2017-05-01 17:04:50 -0700 | [diff] [blame] | 6522 | return 0; |
| 6523 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6524 | |
| 6525 | err_nand_manuf_cleanup: |
| 6526 | nand_manufacturer_cleanup(chip); |
| 6527 | |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 6528 | err_free_buf: |
| 6529 | kfree(chip->data_buf); |
| 6530 | kfree(ecc->code_buf); |
| 6531 | kfree(ecc->calc_buf); |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 6532 | |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 6533 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6534 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 6535 | EXPORT_SYMBOL(nand_scan_tail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6536 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 6537 | /* |
| 6538 | * is_module_text_address() isn't exported, and it's mostly a pointless |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 6539 | * test if this is a module _anyway_ -- they'd have to try _really_ hard |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 6540 | * to call us from in-kernel code if the core NAND support is modular. |
| 6541 | */ |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 6542 | #ifdef MODULE |
| 6543 | #define caller_is_module() (1) |
| 6544 | #else |
| 6545 | #define caller_is_module() \ |
Rusty Russell | a6e6abd | 2009-03-31 13:05:31 -0600 | [diff] [blame] | 6546 | is_module_text_address((unsigned long)__builtin_return_address(0)) |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 6547 | #endif |
| 6548 | |
| 6549 | /** |
| 6550 | * nand_scan - [NAND Interface] Scan for the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 6551 | * @mtd: MTD device structure |
| 6552 | * @maxchips: number of chips to scan for |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 6553 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 6554 | * This fills out all the uninitialized function pointers with the defaults. |
| 6555 | * 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] | 6556 | * appropriate values. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 6557 | */ |
| 6558 | int nand_scan(struct mtd_info *mtd, int maxchips) |
| 6559 | { |
| 6560 | int ret; |
| 6561 | |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 6562 | ret = nand_scan_ident(mtd, maxchips, NULL); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 6563 | if (!ret) |
| 6564 | ret = nand_scan_tail(mtd); |
| 6565 | return ret; |
| 6566 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 6567 | EXPORT_SYMBOL(nand_scan); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 6568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6569 | /** |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 6570 | * nand_cleanup - [NAND Interface] Free resources held by the NAND device |
| 6571 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 6572 | */ |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 6573 | void nand_cleanup(struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6574 | { |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 6575 | if (chip->ecc.mode == NAND_ECC_SOFT && |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 6576 | chip->ecc.algo == NAND_ECC_BCH) |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 6577 | nand_bch_free((struct nand_bch_control *)chip->ecc.priv); |
| 6578 | |
Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 6579 | /* Free bad block table memory */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 6580 | kfree(chip->bbt); |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 6581 | kfree(chip->data_buf); |
| 6582 | kfree(chip->ecc.code_buf); |
| 6583 | kfree(chip->ecc.calc_buf); |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 6584 | |
| 6585 | /* Free bad block descriptor memory */ |
| 6586 | if (chip->badblock_pattern && chip->badblock_pattern->options |
| 6587 | & NAND_BBT_DYNAMICSTRUCT) |
| 6588 | kfree(chip->badblock_pattern); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 6589 | |
| 6590 | /* Free manufacturer priv data. */ |
| 6591 | nand_manufacturer_cleanup(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6592 | } |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 6593 | EXPORT_SYMBOL_GPL(nand_cleanup); |
| 6594 | |
| 6595 | /** |
| 6596 | * nand_release - [NAND Interface] Unregister the MTD device and free resources |
| 6597 | * held by the NAND device |
| 6598 | * @mtd: MTD device structure |
| 6599 | */ |
| 6600 | void nand_release(struct mtd_info *mtd) |
| 6601 | { |
| 6602 | mtd_device_unregister(mtd); |
| 6603 | nand_cleanup(mtd_to_nand(mtd)); |
| 6604 | } |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 6605 | EXPORT_SYMBOL_GPL(nand_release); |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 6606 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 6607 | MODULE_LICENSE("GPL"); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 6608 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>"); |
| 6609 | MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>"); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 6610 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |