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; |
| 68 | oobregion->length = 4; |
| 69 | } else { |
| 70 | oobregion->offset = 6; |
| 71 | oobregion->length = ecc->total - 4; |
| 72 | } |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section, |
| 78 | struct mtd_oob_region *oobregion) |
| 79 | { |
| 80 | if (section > 1) |
| 81 | return -ERANGE; |
| 82 | |
| 83 | if (mtd->oobsize == 16) { |
| 84 | if (section) |
| 85 | return -ERANGE; |
| 86 | |
| 87 | oobregion->length = 8; |
| 88 | oobregion->offset = 8; |
| 89 | } else { |
| 90 | oobregion->length = 2; |
| 91 | if (!section) |
| 92 | oobregion->offset = 3; |
| 93 | else |
| 94 | oobregion->offset = 6; |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = { |
| 101 | .ecc = nand_ooblayout_ecc_sp, |
| 102 | .free = nand_ooblayout_free_sp, |
| 103 | }; |
| 104 | EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops); |
| 105 | |
| 106 | static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section, |
| 107 | struct mtd_oob_region *oobregion) |
| 108 | { |
| 109 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 110 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 111 | |
| 112 | if (section) |
| 113 | return -ERANGE; |
| 114 | |
| 115 | oobregion->length = ecc->total; |
| 116 | oobregion->offset = mtd->oobsize - oobregion->length; |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section, |
| 122 | struct mtd_oob_region *oobregion) |
| 123 | { |
| 124 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 125 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 126 | |
| 127 | if (section) |
| 128 | return -ERANGE; |
| 129 | |
| 130 | oobregion->length = mtd->oobsize - ecc->total - 2; |
| 131 | oobregion->offset = 2; |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = { |
| 137 | .ecc = nand_ooblayout_ecc_lp, |
| 138 | .free = nand_ooblayout_free_lp, |
| 139 | }; |
| 140 | EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops); |
Thomas Gleixner | d470a97 | 2006-05-23 23:48:57 +0200 | [diff] [blame] | 141 | |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 142 | /* |
| 143 | * Support the old "large page" layout used for 1-bit Hamming ECC where ECC |
| 144 | * are placed at a fixed offset. |
| 145 | */ |
| 146 | static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section, |
| 147 | struct mtd_oob_region *oobregion) |
| 148 | { |
| 149 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 150 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 151 | |
| 152 | if (section) |
| 153 | return -ERANGE; |
| 154 | |
| 155 | switch (mtd->oobsize) { |
| 156 | case 64: |
| 157 | oobregion->offset = 40; |
| 158 | break; |
| 159 | case 128: |
| 160 | oobregion->offset = 80; |
| 161 | break; |
| 162 | default: |
| 163 | return -EINVAL; |
| 164 | } |
| 165 | |
| 166 | oobregion->length = ecc->total; |
| 167 | if (oobregion->offset + oobregion->length > mtd->oobsize) |
| 168 | return -ERANGE; |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section, |
| 174 | struct mtd_oob_region *oobregion) |
| 175 | { |
| 176 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 177 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 178 | int ecc_offset = 0; |
| 179 | |
| 180 | if (section < 0 || section > 1) |
| 181 | return -ERANGE; |
| 182 | |
| 183 | switch (mtd->oobsize) { |
| 184 | case 64: |
| 185 | ecc_offset = 40; |
| 186 | break; |
| 187 | case 128: |
| 188 | ecc_offset = 80; |
| 189 | break; |
| 190 | default: |
| 191 | return -EINVAL; |
| 192 | } |
| 193 | |
| 194 | if (section == 0) { |
| 195 | oobregion->offset = 2; |
| 196 | oobregion->length = ecc_offset - 2; |
| 197 | } else { |
| 198 | oobregion->offset = ecc_offset + ecc->total; |
| 199 | oobregion->length = mtd->oobsize - oobregion->offset; |
| 200 | } |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
Colin Ian King | d4ed3b9 | 2017-05-04 13:11:00 +0100 | [diff] [blame] | 205 | static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = { |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 206 | .ecc = nand_ooblayout_ecc_lp_hamming, |
| 207 | .free = nand_ooblayout_free_lp_hamming, |
| 208 | }; |
| 209 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 210 | static int check_offs_len(struct mtd_info *mtd, |
| 211 | loff_t ofs, uint64_t len) |
| 212 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 213 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 214 | int ret = 0; |
| 215 | |
| 216 | /* Start address must align on block boundary */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 217 | if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 218 | pr_debug("%s: unaligned address\n", __func__); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 219 | ret = -EINVAL; |
| 220 | } |
| 221 | |
| 222 | /* Length must align on block boundary */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 223 | if (len & ((1ULL << chip->phys_erase_shift) - 1)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 224 | pr_debug("%s: length not block aligned\n", __func__); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 225 | ret = -EINVAL; |
| 226 | } |
| 227 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 228 | return ret; |
| 229 | } |
| 230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | /** |
| 232 | * nand_release_device - [GENERIC] release chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 233 | * @mtd: MTD device structure |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 234 | * |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 235 | * Release chip lock and wake up anyone waiting on the device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 237 | static void nand_release_device(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 239 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 241 | /* Release the controller and the chip */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 242 | spin_lock(&chip->controller->lock); |
| 243 | chip->controller->active = NULL; |
| 244 | chip->state = FL_READY; |
| 245 | wake_up(&chip->controller->wq); |
| 246 | spin_unlock(&chip->controller->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
| 250 | * nand_read_byte - [DEFAULT] read one byte from the chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 251 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 253 | * Default read function for 8bit buswidth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 255 | static uint8_t nand_read_byte(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 257 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 258 | return readb(chip->IO_ADDR_R); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /** |
Masanari Iida | 064a769 | 2012-11-09 23:20:58 +0900 | [diff] [blame] | 262 | * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 263 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 265 | * Default read function for 16bit buswidth with endianness conversion. |
| 266 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 268 | static uint8_t nand_read_byte16(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 270 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 271 | return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | * nand_read_word - [DEFAULT] read one word from the chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 276 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 278 | * Default read function for 16bit buswidth without endianness conversion. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | */ |
| 280 | static u16 nand_read_word(struct mtd_info *mtd) |
| 281 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 282 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 283 | return readw(chip->IO_ADDR_R); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | * nand_select_chip - [DEFAULT] control CE line |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 288 | * @mtd: MTD device structure |
| 289 | * @chipnr: chipnumber to select, -1 for deselect |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | * |
| 291 | * Default select function for 1 chip devices. |
| 292 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 293 | static void nand_select_chip(struct mtd_info *mtd, int chipnr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 295 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 296 | |
| 297 | switch (chipnr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | case -1: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 299 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | break; |
| 301 | case 0: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | break; |
| 303 | |
| 304 | default: |
| 305 | BUG(); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /** |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 310 | * nand_write_byte - [DEFAULT] write single byte to chip |
| 311 | * @mtd: MTD device structure |
| 312 | * @byte: value to write |
| 313 | * |
| 314 | * Default function to write a byte to I/O[7:0] |
| 315 | */ |
| 316 | static void nand_write_byte(struct mtd_info *mtd, uint8_t byte) |
| 317 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 318 | struct nand_chip *chip = mtd_to_nand(mtd); |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 319 | |
| 320 | chip->write_buf(mtd, &byte, 1); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16 |
| 325 | * @mtd: MTD device structure |
| 326 | * @byte: value to write |
| 327 | * |
| 328 | * Default function to write a byte to I/O[7:0] on a 16-bit wide chip. |
| 329 | */ |
| 330 | static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte) |
| 331 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 332 | struct nand_chip *chip = mtd_to_nand(mtd); |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 333 | uint16_t word = byte; |
| 334 | |
| 335 | /* |
| 336 | * It's not entirely clear what should happen to I/O[15:8] when writing |
| 337 | * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads: |
| 338 | * |
| 339 | * When the host supports a 16-bit bus width, only data is |
| 340 | * transferred at the 16-bit width. All address and command line |
| 341 | * transfers shall use only the lower 8-bits of the data bus. During |
| 342 | * command transfers, the host may place any value on the upper |
| 343 | * 8-bits of the data bus. During address transfers, the host shall |
| 344 | * set the upper 8-bits of the data bus to 00h. |
| 345 | * |
| 346 | * One user of the write_byte callback is nand_onfi_set_features. The |
| 347 | * four parameters are specified to be written to I/O[7:0], but this is |
| 348 | * neither an address nor a command transfer. Let's assume a 0 on the |
| 349 | * upper I/O lines is OK. |
| 350 | */ |
| 351 | chip->write_buf(mtd, (uint8_t *)&word, 2); |
| 352 | } |
| 353 | |
| 354 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | * nand_write_buf - [DEFAULT] write buffer to chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 356 | * @mtd: MTD device structure |
| 357 | * @buf: data buffer |
| 358 | * @len: number of bytes to write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 360 | * Default write function for 8bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 362 | 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] | 363 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 364 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
Alexander Shiyan | 7641383 | 2013-04-13 09:32:13 +0400 | [diff] [blame] | 366 | iowrite8_rep(chip->IO_ADDR_W, buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 370 | * nand_read_buf - [DEFAULT] read chip data into buffer |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 371 | * @mtd: MTD device structure |
| 372 | * @buf: buffer to store date |
| 373 | * @len: number of bytes to read |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 375 | * Default read function for 8bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 377 | 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] | 378 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 379 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
Alexander Shiyan | 7641383 | 2013-04-13 09:32:13 +0400 | [diff] [blame] | 381 | ioread8_rep(chip->IO_ADDR_R, buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | * nand_write_buf16 - [DEFAULT] write buffer to chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 386 | * @mtd: MTD device structure |
| 387 | * @buf: data buffer |
| 388 | * @len: number of bytes to write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 390 | * Default write function for 16bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 392 | 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] | 393 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 394 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | u16 *p = (u16 *) buf; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 396 | |
Alexander Shiyan | 7641383 | 2013-04-13 09:32:13 +0400 | [diff] [blame] | 397 | iowrite16_rep(chip->IO_ADDR_W, p, len >> 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 401 | * nand_read_buf16 - [DEFAULT] read chip data into buffer |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 402 | * @mtd: MTD device structure |
| 403 | * @buf: buffer to store date |
| 404 | * @len: number of bytes to read |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 406 | * Default read function for 16bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 408 | 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] | 409 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 410 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | u16 *p = (u16 *) buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
Alexander Shiyan | 7641383 | 2013-04-13 09:32:13 +0400 | [diff] [blame] | 413 | ioread16_rep(chip->IO_ADDR_R, p, len >> 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 418 | * @mtd: MTD device structure |
| 419 | * @ofs: offset from device start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 421 | * Check, if the block is bad. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | */ |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 423 | static int nand_block_bad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | { |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 425 | int page, page_end, res; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 426 | struct nand_chip *chip = mtd_to_nand(mtd); |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 427 | u8 bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
Brian Norris | 5fb1549 | 2011-05-31 16:31:21 -0700 | [diff] [blame] | 429 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
Kevin Cernekee | b60b08b | 2010-05-04 20:58:10 -0700 | [diff] [blame] | 430 | ofs += mtd->erasesize - mtd->writesize; |
| 431 | |
Thomas Knobloch | 1a12f46 | 2007-05-03 07:39:37 +0100 | [diff] [blame] | 432 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 433 | page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1); |
Thomas Knobloch | 1a12f46 | 2007-05-03 07:39:37 +0100 | [diff] [blame] | 434 | |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 435 | for (; page < page_end; page++) { |
| 436 | res = chip->ecc.read_oob(mtd, chip, page); |
| 437 | if (res) |
| 438 | return res; |
| 439 | |
| 440 | bad = chip->oob_poi[chip->badblockpos]; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 441 | |
Brian Norris | cdbec05 | 2012-01-13 18:11:48 -0800 | [diff] [blame] | 442 | if (likely(chip->badblockbits == 8)) |
| 443 | res = bad != 0xFF; |
| 444 | else |
| 445 | res = hweight8(bad) < chip->badblockbits; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 446 | if (res) |
| 447 | return res; |
| 448 | } |
Maxim Levitsky | e0b58d0 | 2010-02-22 20:39:38 +0200 | [diff] [blame] | 449 | |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 450 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /** |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 454 | * 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] | 455 | * @mtd: MTD device structure |
| 456 | * @ofs: offset from device start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 458 | * This is the default implementation, which can be overridden by a hardware |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 459 | * specific driver. It provides the details for writing a bad block marker to a |
| 460 | * block. |
| 461 | */ |
| 462 | static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 463 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 464 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 465 | struct mtd_oob_ops ops; |
| 466 | uint8_t buf[2] = { 0, 0 }; |
| 467 | int ret = 0, res, i = 0; |
| 468 | |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 469 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 470 | ops.oobbuf = buf; |
| 471 | ops.ooboffs = chip->badblockpos; |
| 472 | if (chip->options & NAND_BUSWIDTH_16) { |
| 473 | ops.ooboffs &= ~0x01; |
| 474 | ops.len = ops.ooblen = 2; |
| 475 | } else { |
| 476 | ops.len = ops.ooblen = 1; |
| 477 | } |
| 478 | ops.mode = MTD_OPS_PLACE_OOB; |
| 479 | |
| 480 | /* Write to first/last page(s) if necessary */ |
| 481 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
| 482 | ofs += mtd->erasesize - mtd->writesize; |
| 483 | do { |
| 484 | res = nand_do_write_oob(mtd, ofs, &ops); |
| 485 | if (!ret) |
| 486 | ret = res; |
| 487 | |
| 488 | i++; |
| 489 | ofs += mtd->writesize; |
| 490 | } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2); |
| 491 | |
| 492 | return ret; |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * nand_block_markbad_lowlevel - mark a block bad |
| 497 | * @mtd: MTD device structure |
| 498 | * @ofs: offset from device start |
| 499 | * |
| 500 | * This function performs the generic NAND bad block marking steps (i.e., bad |
| 501 | * block table(s) and/or marker(s)). We only allow the hardware driver to |
| 502 | * specify how to write bad block markers to OOB (chip->block_markbad). |
| 503 | * |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 504 | * We try operations in the following order: |
Mauro Carvalho Chehab | b6f6c29 | 2017-05-13 07:40:36 -0300 | [diff] [blame] | 505 | * |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 506 | * (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] | 507 | * (2) write bad block marker to OOB area of affected block (unless flag |
| 508 | * NAND_BBT_NO_OOB_BBM is present) |
| 509 | * (3) update the BBT |
Mauro Carvalho Chehab | b6f6c29 | 2017-05-13 07:40:36 -0300 | [diff] [blame] | 510 | * |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 511 | * 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] | 512 | * procedures, and dump the error in the end. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | */ |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 514 | 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] | 515 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 516 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 517 | int res, ret = 0; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 518 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 519 | if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) { |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 520 | struct erase_info einfo; |
| 521 | |
| 522 | /* Attempt erase before marking OOB */ |
| 523 | memset(&einfo, 0, sizeof(einfo)); |
| 524 | einfo.mtd = mtd; |
| 525 | einfo.addr = ofs; |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 526 | einfo.len = 1ULL << chip->phys_erase_shift; |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 527 | nand_erase_nand(mtd, &einfo, 0); |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 528 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 529 | /* Write bad block marker to OOB */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 530 | nand_get_device(mtd, FL_WRITING); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 531 | ret = chip->block_markbad(mtd, ofs); |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 532 | nand_release_device(mtd); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 533 | } |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 534 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 535 | /* Mark block bad in BBT */ |
| 536 | if (chip->bbt) { |
| 537 | res = nand_markbad_bbt(mtd, ofs); |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 538 | if (!ret) |
| 539 | ret = res; |
| 540 | } |
| 541 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 542 | if (!ret) |
| 543 | mtd->ecc_stats.badblocks++; |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 544 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 545 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 548 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | * nand_check_wp - [GENERIC] check if the chip is write protected |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 550 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 552 | * Check, if the device is write protected. The function expects, that the |
| 553 | * device is already selected. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 555 | static int nand_check_wp(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 557 | struct nand_chip *chip = mtd_to_nand(mtd); |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 558 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 559 | /* Broken xD cards report WP despite being writable */ |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 560 | if (chip->options & NAND_BROKEN_XD) |
| 561 | return 0; |
| 562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | /* Check the WP bit */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 564 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
| 565 | return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /** |
Gu Zheng | c30e1f7 | 2014-09-03 17:49:10 +0800 | [diff] [blame] | 569 | * nand_block_isreserved - [GENERIC] Check if a block is marked reserved. |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 570 | * @mtd: MTD device structure |
| 571 | * @ofs: offset from device start |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 572 | * |
Gu Zheng | c30e1f7 | 2014-09-03 17:49:10 +0800 | [diff] [blame] | 573 | * Check if the block is marked as reserved. |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 574 | */ |
| 575 | static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
| 576 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 577 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 578 | |
| 579 | if (!chip->bbt) |
| 580 | return 0; |
| 581 | /* Return info from the table */ |
| 582 | return nand_isreserved_bbt(mtd, ofs); |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad |
| 587 | * @mtd: MTD device structure |
| 588 | * @ofs: offset from device start |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 589 | * @allowbbt: 1, if its allowed to access the bbt area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | * |
| 591 | * Check, if the block is bad. Either by reading the bad block table or |
| 592 | * calling of the scan function. |
| 593 | */ |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 594 | 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] | 595 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 596 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 597 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 598 | if (!chip->bbt) |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 599 | return chip->block_bad(mtd, ofs); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | /* Return info from the table */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 602 | return nand_isbad_bbt(mtd, ofs, allowbbt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 605 | /** |
| 606 | * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands. |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 607 | * @mtd: MTD device structure |
| 608 | * @timeo: Timeout |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 609 | * |
| 610 | * Helper function for nand_wait_ready used when needing to wait in interrupt |
| 611 | * context. |
| 612 | */ |
| 613 | static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo) |
| 614 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 615 | struct nand_chip *chip = mtd_to_nand(mtd); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 616 | int i; |
| 617 | |
| 618 | /* Wait for the device to get ready */ |
| 619 | for (i = 0; i < timeo; i++) { |
| 620 | if (chip->dev_ready(mtd)) |
| 621 | break; |
| 622 | touch_softlockup_watchdog(); |
| 623 | mdelay(1); |
| 624 | } |
| 625 | } |
| 626 | |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 627 | /** |
| 628 | * nand_wait_ready - [GENERIC] Wait for the ready pin after commands. |
| 629 | * @mtd: MTD device structure |
| 630 | * |
| 631 | * Wait for the ready pin after a command, and warn if a timeout occurs. |
| 632 | */ |
David Woodhouse | 4b648b0 | 2006-09-25 17:05:24 +0100 | [diff] [blame] | 633 | void nand_wait_ready(struct mtd_info *mtd) |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 634 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 635 | struct nand_chip *chip = mtd_to_nand(mtd); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 636 | unsigned long timeo = 400; |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 637 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 638 | if (in_interrupt() || oops_in_progress) |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 639 | return panic_nand_wait_ready(mtd, timeo); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 640 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 641 | /* Wait until command is processed or timeout occurs */ |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 642 | timeo = jiffies + msecs_to_jiffies(timeo); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 643 | do { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 644 | if (chip->dev_ready(mtd)) |
Ezequiel Garcia | 4c7e054 | 2016-04-12 17:46:41 -0300 | [diff] [blame] | 645 | return; |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 646 | cond_resched(); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 647 | } while (time_before(jiffies, timeo)); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 648 | |
Brian Norris | 9ebfdf5 | 2016-03-04 17:19:23 -0800 | [diff] [blame] | 649 | if (!chip->dev_ready(mtd)) |
| 650 | pr_warn_ratelimited("timeout while waiting for chip to become ready\n"); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 651 | } |
David Woodhouse | 4b648b0 | 2006-09-25 17:05:24 +0100 | [diff] [blame] | 652 | EXPORT_SYMBOL_GPL(nand_wait_ready); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | /** |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 655 | * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands. |
| 656 | * @mtd: MTD device structure |
| 657 | * @timeo: Timeout in ms |
| 658 | * |
| 659 | * Wait for status ready (i.e. command done) or timeout. |
| 660 | */ |
| 661 | static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo) |
| 662 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 663 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 664 | |
| 665 | timeo = jiffies + msecs_to_jiffies(timeo); |
| 666 | do { |
| 667 | if ((chip->read_byte(mtd) & NAND_STATUS_READY)) |
| 668 | break; |
| 669 | touch_softlockup_watchdog(); |
| 670 | } while (time_before(jiffies, timeo)); |
| 671 | }; |
| 672 | |
| 673 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | * nand_command - [DEFAULT] Send command to NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 675 | * @mtd: MTD device structure |
| 676 | * @command: the command to be sent |
| 677 | * @column: the column address for this command, -1 if none |
| 678 | * @page_addr: the page address for this command, -1 if none |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 680 | * 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] | 681 | * (512 Bytes per page). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 683 | static void nand_command(struct mtd_info *mtd, unsigned int command, |
| 684 | int column, int page_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 686 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 687 | int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 689 | /* Write out the command to the device */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | if (command == NAND_CMD_SEQIN) { |
| 691 | int readcmd; |
| 692 | |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 693 | if (column >= mtd->writesize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | /* OOB area */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 695 | column -= mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | readcmd = NAND_CMD_READOOB; |
| 697 | } else if (column < 256) { |
| 698 | /* First 256 bytes --> READ0 */ |
| 699 | readcmd = NAND_CMD_READ0; |
| 700 | } else { |
| 701 | column -= 256; |
| 702 | readcmd = NAND_CMD_READ1; |
| 703 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 704 | chip->cmd_ctrl(mtd, readcmd, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 705 | ctrl &= ~NAND_CTRL_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 707 | chip->cmd_ctrl(mtd, command, ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 709 | /* Address cycle, when necessary */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 710 | ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE; |
| 711 | /* Serially input address */ |
| 712 | if (column != -1) { |
| 713 | /* Adjust columns for 16 bit buswidth */ |
Brian Norris | 3dad234 | 2014-01-29 14:08:12 -0800 | [diff] [blame] | 714 | if (chip->options & NAND_BUSWIDTH_16 && |
| 715 | !nand_opcode_8bits(command)) |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 716 | column >>= 1; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 717 | chip->cmd_ctrl(mtd, column, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 718 | ctrl &= ~NAND_CTRL_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | } |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 720 | if (page_addr != -1) { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 721 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 722 | ctrl &= ~NAND_CTRL_CHANGE; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 723 | chip->cmd_ctrl(mtd, page_addr >> 8, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 724 | /* One more address cycle for devices > 32MiB */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 725 | if (chip->chipsize > (32 << 20)) |
| 726 | chip->cmd_ctrl(mtd, page_addr >> 16, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 727 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 728 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 729 | |
| 730 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 731 | * Program and erase have their own busy handlers status and sequential |
| 732 | * in needs no delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 733 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | switch (command) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 735 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | case NAND_CMD_PAGEPROG: |
| 737 | case NAND_CMD_ERASE1: |
| 738 | case NAND_CMD_ERASE2: |
| 739 | case NAND_CMD_SEQIN: |
| 740 | case NAND_CMD_STATUS: |
Masahiro Yamada | 3158fa0 | 2017-03-23 09:17:49 +0900 | [diff] [blame] | 741 | case NAND_CMD_READID: |
Masahiro Yamada | c5d664a | 2017-03-23 09:17:50 +0900 | [diff] [blame] | 742 | case NAND_CMD_SET_FEATURES: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | return; |
| 744 | |
| 745 | case NAND_CMD_RESET: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 746 | if (chip->dev_ready) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | break; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 748 | udelay(chip->chip_delay); |
| 749 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 750 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 12efdde | 2006-05-24 22:57:09 +0200 | [diff] [blame] | 751 | chip->cmd_ctrl(mtd, |
| 752 | NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 753 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 754 | nand_wait_status_ready(mtd, 250); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | return; |
| 756 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 757 | /* This applies to read commands */ |
Boris Brezillon | 2165c4a | 2017-05-16 18:35:45 +0200 | [diff] [blame] | 758 | case NAND_CMD_READ0: |
| 759 | /* |
| 760 | * READ0 is sometimes used to exit GET STATUS mode. When this |
| 761 | * is the case no address cycles are requested, and we can use |
| 762 | * this information to detect that we should not wait for the |
| 763 | * device to be ready. |
| 764 | */ |
| 765 | if (column == -1 && page_addr == -1) |
| 766 | return; |
| 767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | default: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 769 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | * If we don't have access to the busy pin, we apply the given |
| 771 | * command delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 772 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 773 | if (!chip->dev_ready) { |
| 774 | udelay(chip->chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | return; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 776 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 778 | /* |
| 779 | * Apply this short delay always to ensure that we do wait tWB in |
| 780 | * any case on any machine. |
| 781 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 782 | ndelay(100); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 783 | |
| 784 | nand_wait_ready(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 787 | static void nand_ccs_delay(struct nand_chip *chip) |
| 788 | { |
| 789 | /* |
| 790 | * The controller already takes care of waiting for tCCS when the RNDIN |
| 791 | * or RNDOUT command is sent, return directly. |
| 792 | */ |
| 793 | if (!(chip->options & NAND_WAIT_TCCS)) |
| 794 | return; |
| 795 | |
| 796 | /* |
| 797 | * Wait tCCS_min if it is correctly defined, otherwise wait 500ns |
| 798 | * (which should be safe for all NANDs). |
| 799 | */ |
| 800 | if (chip->data_interface && chip->data_interface->timings.sdr.tCCS_min) |
| 801 | ndelay(chip->data_interface->timings.sdr.tCCS_min / 1000); |
| 802 | else |
| 803 | ndelay(500); |
| 804 | } |
| 805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | /** |
| 807 | * nand_command_lp - [DEFAULT] Send command to NAND large page device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 808 | * @mtd: MTD device structure |
| 809 | * @command: the command to be sent |
| 810 | * @column: the column address for this command, -1 if none |
| 811 | * @page_addr: the page address for this command, -1 if none |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | * |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 813 | * 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] | 814 | * devices. We don't have the separate regions as we have in the small page |
| 815 | * devices. We must emulate NAND_CMD_READOOB to keep the code compatible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 817 | static void nand_command_lp(struct mtd_info *mtd, unsigned int command, |
| 818 | int column, int page_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 820 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | |
| 822 | /* Emulate NAND_CMD_READOOB */ |
| 823 | if (command == NAND_CMD_READOOB) { |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 824 | column += mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | command = NAND_CMD_READ0; |
| 826 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 827 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 828 | /* Command latch cycle */ |
Alexander Shiyan | fb066ad | 2013-02-28 12:02:19 +0400 | [diff] [blame] | 829 | chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | |
| 831 | if (column != -1 || page_addr != -1) { |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 832 | int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | |
| 834 | /* Serially input address */ |
| 835 | if (column != -1) { |
| 836 | /* Adjust columns for 16 bit buswidth */ |
Brian Norris | 3dad234 | 2014-01-29 14:08:12 -0800 | [diff] [blame] | 837 | if (chip->options & NAND_BUSWIDTH_16 && |
| 838 | !nand_opcode_8bits(command)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | column >>= 1; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 840 | chip->cmd_ctrl(mtd, column, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 841 | ctrl &= ~NAND_CTRL_CHANGE; |
Boris Brezillon | fde85cf | 2016-06-15 13:09:51 +0200 | [diff] [blame] | 842 | |
Brian Norris | f5b88de | 2016-10-03 09:49:35 -0700 | [diff] [blame] | 843 | /* Only output a single addr cycle for 8bits opcodes. */ |
Boris Brezillon | fde85cf | 2016-06-15 13:09:51 +0200 | [diff] [blame] | 844 | if (!nand_opcode_8bits(command)) |
| 845 | chip->cmd_ctrl(mtd, column >> 8, ctrl); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 846 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | if (page_addr != -1) { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 848 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
| 849 | chip->cmd_ctrl(mtd, page_addr >> 8, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 850 | NAND_NCE | NAND_ALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | /* One more address cycle for devices > 128MiB */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 852 | if (chip->chipsize > (128 << 20)) |
| 853 | chip->cmd_ctrl(mtd, page_addr >> 16, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 854 | NAND_NCE | NAND_ALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 857 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 858 | |
| 859 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 860 | * Program and erase have their own busy handlers status, sequential |
Gerhard Sittig | 7a442f1 | 2014-03-29 14:36:22 +0100 | [diff] [blame] | 861 | * in and status need no delay. |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 862 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | switch (command) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 864 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | case NAND_CMD_CACHEDPROG: |
| 866 | case NAND_CMD_PAGEPROG: |
| 867 | case NAND_CMD_ERASE1: |
| 868 | case NAND_CMD_ERASE2: |
| 869 | case NAND_CMD_SEQIN: |
| 870 | case NAND_CMD_STATUS: |
Masahiro Yamada | 3158fa0 | 2017-03-23 09:17:49 +0900 | [diff] [blame] | 871 | case NAND_CMD_READID: |
Masahiro Yamada | c5d664a | 2017-03-23 09:17:50 +0900 | [diff] [blame] | 872 | case NAND_CMD_SET_FEATURES: |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 873 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 875 | case NAND_CMD_RNDIN: |
| 876 | nand_ccs_delay(chip); |
| 877 | return; |
| 878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | case NAND_CMD_RESET: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 880 | if (chip->dev_ready) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | break; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 882 | udelay(chip->chip_delay); |
Thomas Gleixner | 12efdde | 2006-05-24 22:57:09 +0200 | [diff] [blame] | 883 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
| 884 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 885 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 886 | NAND_NCE | NAND_CTRL_CHANGE); |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 887 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 888 | nand_wait_status_ready(mtd, 250); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | return; |
| 890 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 891 | case NAND_CMD_RNDOUT: |
| 892 | /* No ready / busy check necessary */ |
| 893 | chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, |
| 894 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 895 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 896 | NAND_NCE | NAND_CTRL_CHANGE); |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 897 | |
| 898 | nand_ccs_delay(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 899 | return; |
| 900 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | case NAND_CMD_READ0: |
Boris Brezillon | 2165c4a | 2017-05-16 18:35:45 +0200 | [diff] [blame] | 902 | /* |
| 903 | * READ0 is sometimes used to exit GET STATUS mode. When this |
| 904 | * is the case no address cycles are requested, and we can use |
| 905 | * this information to detect that READSTART should not be |
| 906 | * issued. |
| 907 | */ |
| 908 | if (column == -1 && page_addr == -1) |
| 909 | return; |
| 910 | |
Thomas Gleixner | 12efdde | 2006-05-24 22:57:09 +0200 | [diff] [blame] | 911 | chip->cmd_ctrl(mtd, NAND_CMD_READSTART, |
| 912 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 913 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 914 | NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 915 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 916 | /* This applies to read commands */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | default: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 918 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | * 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] | 920 | * command delay. |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 921 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 922 | if (!chip->dev_ready) { |
| 923 | udelay(chip->chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | return; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 925 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | } |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 927 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 928 | /* |
| 929 | * Apply this short delay always to ensure that we do wait tWB in |
| 930 | * any case on any machine. |
| 931 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 932 | ndelay(100); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 933 | |
| 934 | nand_wait_ready(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 938 | * panic_nand_get_device - [GENERIC] Get chip for selected access |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 939 | * @chip: the nand chip descriptor |
| 940 | * @mtd: MTD device structure |
| 941 | * @new_state: the state which is requested |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 942 | * |
| 943 | * Used when in panic, no locks are taken. |
| 944 | */ |
| 945 | static void panic_nand_get_device(struct nand_chip *chip, |
| 946 | struct mtd_info *mtd, int new_state) |
| 947 | { |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 948 | /* Hardware controller shared among independent devices */ |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 949 | chip->controller->active = chip; |
| 950 | chip->state = new_state; |
| 951 | } |
| 952 | |
| 953 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | * nand_get_device - [GENERIC] Get chip for selected access |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 955 | * @mtd: MTD device structure |
| 956 | * @new_state: the state which is requested |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | * |
| 958 | * Get the device and lock it for exclusive access |
| 959 | */ |
Thomas Gleixner | 2c0a2be | 2006-05-23 11:50:56 +0200 | [diff] [blame] | 960 | static int |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 961 | nand_get_device(struct mtd_info *mtd, int new_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 963 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 964 | spinlock_t *lock = &chip->controller->lock; |
| 965 | wait_queue_head_t *wq = &chip->controller->wq; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 966 | DECLARE_WAITQUEUE(wait, current); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 967 | retry: |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 968 | spin_lock(lock); |
| 969 | |
vimal singh | b8b3ee9 | 2009-07-09 20:41:22 +0530 | [diff] [blame] | 970 | /* Hardware controller shared among independent devices */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 971 | if (!chip->controller->active) |
| 972 | chip->controller->active = chip; |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 973 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 974 | if (chip->controller->active == chip && chip->state == FL_READY) { |
| 975 | chip->state = new_state; |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 976 | spin_unlock(lock); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 977 | return 0; |
| 978 | } |
| 979 | if (new_state == FL_PM_SUSPENDED) { |
Li Yang | 6b0d9a8 | 2009-11-17 14:45:49 -0800 | [diff] [blame] | 980 | if (chip->controller->active->state == FL_PM_SUSPENDED) { |
| 981 | chip->state = FL_PM_SUSPENDED; |
| 982 | spin_unlock(lock); |
| 983 | return 0; |
Li Yang | 6b0d9a8 | 2009-11-17 14:45:49 -0800 | [diff] [blame] | 984 | } |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 985 | } |
| 986 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 987 | add_wait_queue(wq, &wait); |
| 988 | spin_unlock(lock); |
| 989 | schedule(); |
| 990 | remove_wait_queue(wq, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | goto retry; |
| 992 | } |
| 993 | |
| 994 | /** |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 995 | * panic_nand_wait - [GENERIC] wait until the command is done |
| 996 | * @mtd: MTD device structure |
| 997 | * @chip: NAND chip structure |
| 998 | * @timeo: timeout |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 999 | * |
| 1000 | * Wait for command done. This is a helper function for nand_wait used when |
| 1001 | * 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] | 1002 | * an oops through mtdoops. |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1003 | */ |
| 1004 | static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip, |
| 1005 | unsigned long timeo) |
| 1006 | { |
| 1007 | int i; |
| 1008 | for (i = 0; i < timeo; i++) { |
| 1009 | if (chip->dev_ready) { |
| 1010 | if (chip->dev_ready(mtd)) |
| 1011 | break; |
| 1012 | } else { |
| 1013 | if (chip->read_byte(mtd) & NAND_STATUS_READY) |
| 1014 | break; |
| 1015 | } |
| 1016 | mdelay(1); |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 1017 | } |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | /** |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1021 | * nand_wait - [DEFAULT] wait until the command is done |
| 1022 | * @mtd: MTD device structure |
| 1023 | * @chip: NAND chip structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | * |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1025 | * Wait for command done. This applies to erase and program only. |
Randy Dunlap | 844d3b4 | 2006-06-28 21:48:27 -0700 | [diff] [blame] | 1026 | */ |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 1027 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | { |
| 1029 | |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1030 | int status; |
| 1031 | unsigned long timeo = 400; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1033 | /* |
| 1034 | * Apply this short delay always to ensure that we do wait tWB in any |
| 1035 | * case on any machine. |
| 1036 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1037 | ndelay(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | |
Artem Bityutskiy | 14c6578 | 2013-03-04 14:21:34 +0200 | [diff] [blame] | 1039 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1041 | if (in_interrupt() || oops_in_progress) |
| 1042 | panic_nand_wait(mtd, chip, timeo); |
| 1043 | else { |
Huang Shijie | 6d2559f | 2013-01-30 10:03:56 +0800 | [diff] [blame] | 1044 | timeo = jiffies + msecs_to_jiffies(timeo); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1045 | do { |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1046 | if (chip->dev_ready) { |
| 1047 | if (chip->dev_ready(mtd)) |
| 1048 | break; |
| 1049 | } else { |
| 1050 | if (chip->read_byte(mtd) & NAND_STATUS_READY) |
| 1051 | break; |
| 1052 | } |
| 1053 | cond_resched(); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1054 | } while (time_before(jiffies, timeo)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | } |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 1056 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1057 | status = (int)chip->read_byte(mtd); |
Matthieu CASTET | f251b8d | 2012-11-05 15:00:44 +0100 | [diff] [blame] | 1058 | /* This can happen if in case of timeout or buggy dev_ready */ |
| 1059 | WARN_ON(!(status & NAND_STATUS_READY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | return status; |
| 1061 | } |
| 1062 | |
| 1063 | /** |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1064 | * nand_reset_data_interface - Reset data interface and timings |
| 1065 | * @chip: The NAND chip |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1066 | * @chipnr: Internal die id |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1067 | * |
| 1068 | * Reset the Data interface and timings to ONFI mode 0. |
| 1069 | * |
| 1070 | * Returns 0 for success or negative error code otherwise. |
| 1071 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1072 | static int nand_reset_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1073 | { |
| 1074 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1075 | const struct nand_data_interface *conf; |
| 1076 | int ret; |
| 1077 | |
| 1078 | if (!chip->setup_data_interface) |
| 1079 | return 0; |
| 1080 | |
| 1081 | /* |
| 1082 | * The ONFI specification says: |
| 1083 | * " |
| 1084 | * To transition from NV-DDR or NV-DDR2 to the SDR data |
| 1085 | * interface, the host shall use the Reset (FFh) command |
| 1086 | * using SDR timing mode 0. A device in any timing mode is |
| 1087 | * required to recognize Reset (FFh) command issued in SDR |
| 1088 | * timing mode 0. |
| 1089 | * " |
| 1090 | * |
| 1091 | * Configure the data interface in SDR mode and set the |
| 1092 | * timings to timing mode 0. |
| 1093 | */ |
| 1094 | |
| 1095 | conf = nand_get_default_data_interface(); |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1096 | ret = chip->setup_data_interface(mtd, chipnr, conf); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1097 | if (ret) |
| 1098 | pr_err("Failed to configure data interface to SDR timing mode 0\n"); |
| 1099 | |
| 1100 | return ret; |
| 1101 | } |
| 1102 | |
| 1103 | /** |
| 1104 | * nand_setup_data_interface - Setup the best data interface and timings |
| 1105 | * @chip: The NAND chip |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1106 | * @chipnr: Internal die id |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1107 | * |
| 1108 | * Find and configure the best data interface and NAND timings supported by |
| 1109 | * the chip and the driver. |
| 1110 | * First tries to retrieve supported timing modes from ONFI information, |
| 1111 | * and if the NAND chip does not support ONFI, relies on the |
| 1112 | * ->onfi_timing_mode_default specified in the nand_ids table. |
| 1113 | * |
| 1114 | * Returns 0 for success or negative error code otherwise. |
| 1115 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1116 | static int nand_setup_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1117 | { |
| 1118 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1119 | int ret; |
| 1120 | |
| 1121 | if (!chip->setup_data_interface || !chip->data_interface) |
| 1122 | return 0; |
| 1123 | |
| 1124 | /* |
| 1125 | * Ensure the timing mode has been changed on the chip side |
| 1126 | * before changing timings on the controller side. |
| 1127 | */ |
| 1128 | if (chip->onfi_version) { |
| 1129 | u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = { |
| 1130 | chip->onfi_timing_mode_default, |
| 1131 | }; |
| 1132 | |
| 1133 | ret = chip->onfi_set_features(mtd, chip, |
| 1134 | ONFI_FEATURE_ADDR_TIMING_MODE, |
| 1135 | tmode_param); |
| 1136 | if (ret) |
| 1137 | goto err; |
| 1138 | } |
| 1139 | |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1140 | ret = chip->setup_data_interface(mtd, chipnr, chip->data_interface); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1141 | err: |
| 1142 | return ret; |
| 1143 | } |
| 1144 | |
| 1145 | /** |
| 1146 | * nand_init_data_interface - find the best data interface and timings |
| 1147 | * @chip: The NAND chip |
| 1148 | * |
| 1149 | * Find the best data interface and NAND timings supported by the chip |
| 1150 | * and the driver. |
| 1151 | * First tries to retrieve supported timing modes from ONFI information, |
| 1152 | * and if the NAND chip does not support ONFI, relies on the |
| 1153 | * ->onfi_timing_mode_default specified in the nand_ids table. After this |
| 1154 | * function nand_chip->data_interface is initialized with the best timing mode |
| 1155 | * available. |
| 1156 | * |
| 1157 | * Returns 0 for success or negative error code otherwise. |
| 1158 | */ |
| 1159 | static int nand_init_data_interface(struct nand_chip *chip) |
| 1160 | { |
| 1161 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1162 | int modes, mode, ret; |
| 1163 | |
| 1164 | if (!chip->setup_data_interface) |
| 1165 | return 0; |
| 1166 | |
| 1167 | /* |
| 1168 | * First try to identify the best timings from ONFI parameters and |
| 1169 | * if the NAND does not support ONFI, fallback to the default ONFI |
| 1170 | * timing mode. |
| 1171 | */ |
| 1172 | modes = onfi_get_async_timing_mode(chip); |
| 1173 | if (modes == ONFI_TIMING_MODE_UNKNOWN) { |
| 1174 | if (!chip->onfi_timing_mode_default) |
| 1175 | return 0; |
| 1176 | |
| 1177 | modes = GENMASK(chip->onfi_timing_mode_default, 0); |
| 1178 | } |
| 1179 | |
| 1180 | chip->data_interface = kzalloc(sizeof(*chip->data_interface), |
| 1181 | GFP_KERNEL); |
| 1182 | if (!chip->data_interface) |
| 1183 | return -ENOMEM; |
| 1184 | |
| 1185 | for (mode = fls(modes) - 1; mode >= 0; mode--) { |
| 1186 | ret = onfi_init_data_interface(chip, chip->data_interface, |
| 1187 | NAND_SDR_IFACE, mode); |
| 1188 | if (ret) |
| 1189 | continue; |
| 1190 | |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1191 | /* Pass -1 to only */ |
| 1192 | ret = chip->setup_data_interface(mtd, |
| 1193 | NAND_DATA_IFACE_CHECK_ONLY, |
| 1194 | chip->data_interface); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1195 | if (!ret) { |
| 1196 | chip->onfi_timing_mode_default = mode; |
| 1197 | break; |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
| 1204 | static void nand_release_data_interface(struct nand_chip *chip) |
| 1205 | { |
| 1206 | kfree(chip->data_interface); |
| 1207 | } |
| 1208 | |
| 1209 | /** |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1210 | * nand_reset - Reset and initialize a NAND device |
| 1211 | * @chip: The NAND chip |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1212 | * @chipnr: Internal die id |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1213 | * |
| 1214 | * Returns 0 for success or negative error code otherwise |
| 1215 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1216 | int nand_reset(struct nand_chip *chip, int chipnr) |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1217 | { |
| 1218 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1219 | int ret; |
| 1220 | |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1221 | ret = nand_reset_data_interface(chip, chipnr); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1222 | if (ret) |
| 1223 | return ret; |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1224 | |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1225 | /* |
| 1226 | * The CS line has to be released before we can apply the new NAND |
| 1227 | * interface settings, hence this weird ->select_chip() dance. |
| 1228 | */ |
| 1229 | chip->select_chip(mtd, chipnr); |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1230 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1231 | chip->select_chip(mtd, -1); |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1232 | |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1233 | chip->select_chip(mtd, chipnr); |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1234 | ret = nand_setup_data_interface(chip, chipnr); |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1235 | chip->select_chip(mtd, -1); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1236 | if (ret) |
| 1237 | return ret; |
| 1238 | |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1239 | return 0; |
| 1240 | } |
| 1241 | |
| 1242 | /** |
Boris BREZILLON | 730a43f | 2015-09-03 18:03:38 +0200 | [diff] [blame] | 1243 | * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data |
| 1244 | * @buf: buffer to test |
| 1245 | * @len: buffer length |
| 1246 | * @bitflips_threshold: maximum number of bitflips |
| 1247 | * |
| 1248 | * Check if a buffer contains only 0xff, which means the underlying region |
| 1249 | * has been erased and is ready to be programmed. |
| 1250 | * The bitflips_threshold specify the maximum number of bitflips before |
| 1251 | * considering the region is not erased. |
| 1252 | * Note: The logic of this function has been extracted from the memweight |
| 1253 | * implementation, except that nand_check_erased_buf function exit before |
| 1254 | * testing the whole buffer if the number of bitflips exceed the |
| 1255 | * bitflips_threshold value. |
| 1256 | * |
| 1257 | * Returns a positive number of bitflips less than or equal to |
| 1258 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 1259 | * threshold. |
| 1260 | */ |
| 1261 | static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold) |
| 1262 | { |
| 1263 | const unsigned char *bitmap = buf; |
| 1264 | int bitflips = 0; |
| 1265 | int weight; |
| 1266 | |
| 1267 | for (; len && ((uintptr_t)bitmap) % sizeof(long); |
| 1268 | len--, bitmap++) { |
| 1269 | weight = hweight8(*bitmap); |
| 1270 | bitflips += BITS_PER_BYTE - weight; |
| 1271 | if (unlikely(bitflips > bitflips_threshold)) |
| 1272 | return -EBADMSG; |
| 1273 | } |
| 1274 | |
| 1275 | for (; len >= sizeof(long); |
| 1276 | len -= sizeof(long), bitmap += sizeof(long)) { |
Pavel Machek | 086567f | 2017-04-21 12:51:07 +0200 | [diff] [blame] | 1277 | unsigned long d = *((unsigned long *)bitmap); |
| 1278 | if (d == ~0UL) |
| 1279 | continue; |
| 1280 | weight = hweight_long(d); |
Boris BREZILLON | 730a43f | 2015-09-03 18:03:38 +0200 | [diff] [blame] | 1281 | bitflips += BITS_PER_LONG - weight; |
| 1282 | if (unlikely(bitflips > bitflips_threshold)) |
| 1283 | return -EBADMSG; |
| 1284 | } |
| 1285 | |
| 1286 | for (; len > 0; len--, bitmap++) { |
| 1287 | weight = hweight8(*bitmap); |
| 1288 | bitflips += BITS_PER_BYTE - weight; |
| 1289 | if (unlikely(bitflips > bitflips_threshold)) |
| 1290 | return -EBADMSG; |
| 1291 | } |
| 1292 | |
| 1293 | return bitflips; |
| 1294 | } |
| 1295 | |
| 1296 | /** |
| 1297 | * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only |
| 1298 | * 0xff data |
| 1299 | * @data: data buffer to test |
| 1300 | * @datalen: data length |
| 1301 | * @ecc: ECC buffer |
| 1302 | * @ecclen: ECC length |
| 1303 | * @extraoob: extra OOB buffer |
| 1304 | * @extraooblen: extra OOB length |
| 1305 | * @bitflips_threshold: maximum number of bitflips |
| 1306 | * |
| 1307 | * Check if a data buffer and its associated ECC and OOB data contains only |
| 1308 | * 0xff pattern, which means the underlying region has been erased and is |
| 1309 | * ready to be programmed. |
| 1310 | * The bitflips_threshold specify the maximum number of bitflips before |
| 1311 | * considering the region as not erased. |
| 1312 | * |
| 1313 | * Note: |
| 1314 | * 1/ ECC algorithms are working on pre-defined block sizes which are usually |
| 1315 | * different from the NAND page size. When fixing bitflips, ECC engines will |
| 1316 | * report the number of errors per chunk, and the NAND core infrastructure |
| 1317 | * expect you to return the maximum number of bitflips for the whole page. |
| 1318 | * This is why you should always use this function on a single chunk and |
| 1319 | * not on the whole page. After checking each chunk you should update your |
| 1320 | * max_bitflips value accordingly. |
| 1321 | * 2/ When checking for bitflips in erased pages you should not only check |
| 1322 | * the payload data but also their associated ECC data, because a user might |
| 1323 | * have programmed almost all bits to 1 but a few. In this case, we |
| 1324 | * shouldn't consider the chunk as erased, and checking ECC bytes prevent |
| 1325 | * this case. |
| 1326 | * 3/ The extraoob argument is optional, and should be used if some of your OOB |
| 1327 | * data are protected by the ECC engine. |
| 1328 | * It could also be used if you support subpages and want to attach some |
| 1329 | * extra OOB data to an ECC chunk. |
| 1330 | * |
| 1331 | * Returns a positive number of bitflips less than or equal to |
| 1332 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 1333 | * threshold. In case of success, the passed buffers are filled with 0xff. |
| 1334 | */ |
| 1335 | int nand_check_erased_ecc_chunk(void *data, int datalen, |
| 1336 | void *ecc, int ecclen, |
| 1337 | void *extraoob, int extraooblen, |
| 1338 | int bitflips_threshold) |
| 1339 | { |
| 1340 | int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0; |
| 1341 | |
| 1342 | data_bitflips = nand_check_erased_buf(data, datalen, |
| 1343 | bitflips_threshold); |
| 1344 | if (data_bitflips < 0) |
| 1345 | return data_bitflips; |
| 1346 | |
| 1347 | bitflips_threshold -= data_bitflips; |
| 1348 | |
| 1349 | ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold); |
| 1350 | if (ecc_bitflips < 0) |
| 1351 | return ecc_bitflips; |
| 1352 | |
| 1353 | bitflips_threshold -= ecc_bitflips; |
| 1354 | |
| 1355 | extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen, |
| 1356 | bitflips_threshold); |
| 1357 | if (extraoob_bitflips < 0) |
| 1358 | return extraoob_bitflips; |
| 1359 | |
| 1360 | if (data_bitflips) |
| 1361 | memset(data, 0xff, datalen); |
| 1362 | |
| 1363 | if (ecc_bitflips) |
| 1364 | memset(ecc, 0xff, ecclen); |
| 1365 | |
| 1366 | if (extraoob_bitflips) |
| 1367 | memset(extraoob, 0xff, extraooblen); |
| 1368 | |
| 1369 | return data_bitflips + ecc_bitflips + extraoob_bitflips; |
| 1370 | } |
| 1371 | EXPORT_SYMBOL(nand_check_erased_ecc_chunk); |
| 1372 | |
| 1373 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1374 | * nand_read_page_raw - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1375 | * @mtd: mtd info structure |
| 1376 | * @chip: nand chip info structure |
| 1377 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1378 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1379 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1380 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1381 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1382 | */ |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 1383 | int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 1384 | uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1385 | { |
| 1386 | chip->read_buf(mtd, buf, mtd->writesize); |
Brian Norris | 279f08d | 2012-05-02 10:15:03 -0700 | [diff] [blame] | 1387 | if (oob_required) |
| 1388 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1389 | return 0; |
| 1390 | } |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 1391 | EXPORT_SYMBOL(nand_read_page_raw); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1392 | |
| 1393 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1394 | * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1395 | * @mtd: mtd info structure |
| 1396 | * @chip: nand chip info structure |
| 1397 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1398 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1399 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1400 | * |
| 1401 | * We need a special oob layout and handling even when OOB isn't used. |
| 1402 | */ |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1403 | static int nand_read_page_raw_syndrome(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1404 | struct nand_chip *chip, uint8_t *buf, |
| 1405 | int oob_required, int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1406 | { |
| 1407 | int eccsize = chip->ecc.size; |
| 1408 | int eccbytes = chip->ecc.bytes; |
| 1409 | uint8_t *oob = chip->oob_poi; |
| 1410 | int steps, size; |
| 1411 | |
| 1412 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
| 1413 | chip->read_buf(mtd, buf, eccsize); |
| 1414 | buf += eccsize; |
| 1415 | |
| 1416 | if (chip->ecc.prepad) { |
| 1417 | chip->read_buf(mtd, oob, chip->ecc.prepad); |
| 1418 | oob += chip->ecc.prepad; |
| 1419 | } |
| 1420 | |
| 1421 | chip->read_buf(mtd, oob, eccbytes); |
| 1422 | oob += eccbytes; |
| 1423 | |
| 1424 | if (chip->ecc.postpad) { |
| 1425 | chip->read_buf(mtd, oob, chip->ecc.postpad); |
| 1426 | oob += chip->ecc.postpad; |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | size = mtd->oobsize - (oob - chip->oob_poi); |
| 1431 | if (size) |
| 1432 | chip->read_buf(mtd, oob, size); |
| 1433 | |
| 1434 | return 0; |
| 1435 | } |
| 1436 | |
| 1437 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1438 | * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1439 | * @mtd: mtd info structure |
| 1440 | * @chip: nand chip info structure |
| 1441 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1442 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1443 | * @page: page number to read |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1444 | */ |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1445 | 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] | 1446 | uint8_t *buf, int oob_required, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1448 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1449 | int eccbytes = chip->ecc.bytes; |
| 1450 | int eccsteps = chip->ecc.steps; |
| 1451 | uint8_t *p = buf; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 1452 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1453 | uint8_t *ecc_code = chip->buffers->ecccode; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1454 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1455 | |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1456 | chip->ecc.read_page_raw(mtd, chip, buf, 1, page); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1457 | |
| 1458 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 1459 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1460 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1461 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 1462 | chip->ecc.total); |
| 1463 | if (ret) |
| 1464 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1465 | |
| 1466 | eccsteps = chip->ecc.steps; |
| 1467 | p = buf; |
| 1468 | |
| 1469 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1470 | int stat; |
| 1471 | |
| 1472 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1473 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1474 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1475 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1476 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1477 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1478 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1479 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1480 | return max_bitflips; |
Thomas Gleixner | 22c60f5 | 2005-04-04 19:56:32 +0100 | [diff] [blame] | 1481 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | /** |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 1484 | * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1485 | * @mtd: mtd info structure |
| 1486 | * @chip: nand chip info structure |
| 1487 | * @data_offs: offset of requested data within the page |
| 1488 | * @readlen: data length |
| 1489 | * @bufpoi: buffer to store read data |
Huang Shijie | e004deb | 2014-01-03 11:01:40 +0800 | [diff] [blame] | 1490 | * @page: page number to read |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1491 | */ |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1492 | 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] | 1493 | uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi, |
| 1494 | int page) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1495 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1496 | int start_step, end_step, num_steps, ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1497 | uint8_t *p; |
| 1498 | int data_col_addr, i, gaps = 0; |
| 1499 | int datafrag_len, eccfrag_len, aligned_len, aligned_pos; |
| 1500 | int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1501 | int index, section = 0; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1502 | unsigned int max_bitflips = 0; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1503 | struct mtd_oob_region oobregion = { }; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1504 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1505 | /* Column address within the page aligned to ECC size (256bytes) */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1506 | start_step = data_offs / chip->ecc.size; |
| 1507 | end_step = (data_offs + readlen - 1) / chip->ecc.size; |
| 1508 | num_steps = end_step - start_step + 1; |
Ron | 4a4163ca | 2014-03-16 04:01:07 +1030 | [diff] [blame] | 1509 | index = start_step * chip->ecc.bytes; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1510 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1511 | /* Data size aligned to ECC ecc.size */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1512 | datafrag_len = num_steps * chip->ecc.size; |
| 1513 | eccfrag_len = num_steps * chip->ecc.bytes; |
| 1514 | |
| 1515 | data_col_addr = start_step * chip->ecc.size; |
| 1516 | /* If we read not a page aligned data */ |
| 1517 | if (data_col_addr != 0) |
| 1518 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1); |
| 1519 | |
| 1520 | p = bufpoi + data_col_addr; |
| 1521 | chip->read_buf(mtd, p, datafrag_len); |
| 1522 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1523 | /* Calculate ECC */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1524 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) |
| 1525 | chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]); |
| 1526 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1527 | /* |
| 1528 | * The performance is faster if we position offsets according to |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1529 | * 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] | 1530 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1531 | ret = mtd_ooblayout_find_eccregion(mtd, index, §ion, &oobregion); |
| 1532 | if (ret) |
| 1533 | return ret; |
| 1534 | |
| 1535 | if (oobregion.length < eccfrag_len) |
| 1536 | gaps = 1; |
| 1537 | |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1538 | if (gaps) { |
| 1539 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1); |
| 1540 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1541 | } else { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1542 | /* |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1543 | * Send the command to read the particular ECC bytes take care |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1544 | * about buswidth alignment in read_buf. |
| 1545 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1546 | aligned_pos = oobregion.offset & ~(busw - 1); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1547 | aligned_len = eccfrag_len; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1548 | if (oobregion.offset & (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1549 | aligned_len++; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1550 | if ((oobregion.offset + (num_steps * chip->ecc.bytes)) & |
| 1551 | (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1552 | aligned_len++; |
| 1553 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1554 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1555 | mtd->writesize + aligned_pos, -1); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1556 | chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len); |
| 1557 | } |
| 1558 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1559 | ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode, |
| 1560 | chip->oob_poi, index, eccfrag_len); |
| 1561 | if (ret) |
| 1562 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1563 | |
| 1564 | p = bufpoi + data_col_addr; |
| 1565 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { |
| 1566 | int stat; |
| 1567 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1568 | stat = chip->ecc.correct(mtd, p, |
| 1569 | &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 1570 | if (stat == -EBADMSG && |
| 1571 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1572 | /* check for empty pages with bitflips */ |
| 1573 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 1574 | &chip->buffers->ecccode[i], |
| 1575 | chip->ecc.bytes, |
| 1576 | NULL, 0, |
| 1577 | chip->ecc.strength); |
| 1578 | } |
| 1579 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1580 | if (stat < 0) { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1581 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1582 | } else { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1583 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1584 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1585 | } |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1586 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1587 | return max_bitflips; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1591 | * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1592 | * @mtd: mtd info structure |
| 1593 | * @chip: nand chip info structure |
| 1594 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1595 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1596 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1597 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1598 | * Not for syndrome calculating ECC controllers which need a special oob layout. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1599 | */ |
| 1600 | 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] | 1601 | uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1602 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1603 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1604 | int eccbytes = chip->ecc.bytes; |
| 1605 | int eccsteps = chip->ecc.steps; |
| 1606 | uint8_t *p = buf; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 1607 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1608 | uint8_t *ecc_code = chip->buffers->ecccode; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1609 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1610 | |
| 1611 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1612 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 1613 | chip->read_buf(mtd, p, eccsize); |
| 1614 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1615 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1616 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1617 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1618 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 1619 | chip->ecc.total); |
| 1620 | if (ret) |
| 1621 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1622 | |
| 1623 | eccsteps = chip->ecc.steps; |
| 1624 | p = buf; |
| 1625 | |
| 1626 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1627 | int stat; |
| 1628 | |
| 1629 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 1630 | if (stat == -EBADMSG && |
| 1631 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1632 | /* check for empty pages with bitflips */ |
| 1633 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 1634 | &ecc_code[i], eccbytes, |
| 1635 | NULL, 0, |
| 1636 | chip->ecc.strength); |
| 1637 | } |
| 1638 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1639 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1640 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1641 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1642 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1643 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1644 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1645 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1646 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1650 | * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1651 | * @mtd: mtd info structure |
| 1652 | * @chip: nand chip info structure |
| 1653 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1654 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1655 | * @page: page number to read |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1656 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1657 | * Hardware ECC for large page chips, require OOB to be read first. For this |
| 1658 | * ECC mode, the write_page method is re-used from ECC_HW. These methods |
| 1659 | * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with |
| 1660 | * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from |
| 1661 | * the data area, by overwriting the NAND manufacturer bad block markings. |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1662 | */ |
| 1663 | static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1664 | struct nand_chip *chip, uint8_t *buf, int oob_required, int page) |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1665 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1666 | int i, eccsize = chip->ecc.size, ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1667 | int eccbytes = chip->ecc.bytes; |
| 1668 | int eccsteps = chip->ecc.steps; |
| 1669 | uint8_t *p = buf; |
| 1670 | uint8_t *ecc_code = chip->buffers->ecccode; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1671 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1672 | unsigned int max_bitflips = 0; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1673 | |
| 1674 | /* Read the OOB area first */ |
| 1675 | chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); |
| 1676 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1677 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page); |
| 1678 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1679 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 1680 | chip->ecc.total); |
| 1681 | if (ret) |
| 1682 | return ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1683 | |
| 1684 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1685 | int stat; |
| 1686 | |
| 1687 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 1688 | chip->read_buf(mtd, p, eccsize); |
| 1689 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1690 | |
| 1691 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 1692 | if (stat == -EBADMSG && |
| 1693 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1694 | /* check for empty pages with bitflips */ |
| 1695 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 1696 | &ecc_code[i], eccbytes, |
| 1697 | NULL, 0, |
| 1698 | chip->ecc.strength); |
| 1699 | } |
| 1700 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1701 | if (stat < 0) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1702 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1703 | } else { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1704 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1705 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1706 | } |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1707 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1708 | return max_bitflips; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1712 | * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1713 | * @mtd: mtd info structure |
| 1714 | * @chip: nand chip info structure |
| 1715 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1716 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1717 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1718 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1719 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 1720 | * need a special oob layout and handling. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1721 | */ |
| 1722 | 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] | 1723 | uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1724 | { |
| 1725 | int i, eccsize = chip->ecc.size; |
| 1726 | int eccbytes = chip->ecc.bytes; |
| 1727 | int eccsteps = chip->ecc.steps; |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 1728 | int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1729 | uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1730 | uint8_t *oob = chip->oob_poi; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1731 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1732 | |
| 1733 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1734 | int stat; |
| 1735 | |
| 1736 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 1737 | chip->read_buf(mtd, p, eccsize); |
| 1738 | |
| 1739 | if (chip->ecc.prepad) { |
| 1740 | chip->read_buf(mtd, oob, chip->ecc.prepad); |
| 1741 | oob += chip->ecc.prepad; |
| 1742 | } |
| 1743 | |
| 1744 | chip->ecc.hwctl(mtd, NAND_ECC_READSYN); |
| 1745 | chip->read_buf(mtd, oob, eccbytes); |
| 1746 | stat = chip->ecc.correct(mtd, p, oob, NULL); |
| 1747 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1748 | oob += eccbytes; |
| 1749 | |
| 1750 | if (chip->ecc.postpad) { |
| 1751 | chip->read_buf(mtd, oob, chip->ecc.postpad); |
| 1752 | oob += chip->ecc.postpad; |
| 1753 | } |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 1754 | |
| 1755 | if (stat == -EBADMSG && |
| 1756 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1757 | /* check for empty pages with bitflips */ |
| 1758 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 1759 | oob - eccpadbytes, |
| 1760 | eccpadbytes, |
| 1761 | NULL, 0, |
| 1762 | chip->ecc.strength); |
| 1763 | } |
| 1764 | |
| 1765 | if (stat < 0) { |
| 1766 | mtd->ecc_stats.failed++; |
| 1767 | } else { |
| 1768 | mtd->ecc_stats.corrected += stat; |
| 1769 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1770 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1771 | } |
| 1772 | |
| 1773 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 1774 | i = mtd->oobsize - (oob - chip->oob_poi); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1775 | if (i) |
| 1776 | chip->read_buf(mtd, oob, i); |
| 1777 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1778 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1782 | * nand_transfer_oob - [INTERN] Transfer oob to client buffer |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1783 | * @mtd: mtd info structure |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1784 | * @oob: oob destination address |
| 1785 | * @ops: oob ops structure |
| 1786 | * @len: size of oob to transfer |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1787 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1788 | 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] | 1789 | struct mtd_oob_ops *ops, size_t len) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1790 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1791 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1792 | int ret; |
| 1793 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 1794 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1795 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 1796 | case MTD_OPS_PLACE_OOB: |
| 1797 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1798 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); |
| 1799 | return oob + len; |
| 1800 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1801 | case MTD_OPS_AUTO_OOB: |
| 1802 | ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi, |
| 1803 | ops->ooboffs, len); |
| 1804 | BUG_ON(ret); |
| 1805 | return oob + len; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1806 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1807 | default: |
| 1808 | BUG(); |
| 1809 | } |
| 1810 | return NULL; |
| 1811 | } |
| 1812 | |
| 1813 | /** |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 1814 | * nand_setup_read_retry - [INTERN] Set the READ RETRY mode |
| 1815 | * @mtd: MTD device structure |
| 1816 | * @retry_mode: the retry mode to use |
| 1817 | * |
| 1818 | * Some vendors supply a special command to shift the Vt threshold, to be used |
| 1819 | * when there are too many bitflips in a page (i.e., ECC error). After setting |
| 1820 | * a new threshold, the host should retry reading the page. |
| 1821 | */ |
| 1822 | static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode) |
| 1823 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 1824 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 1825 | |
| 1826 | pr_debug("setting READ RETRY mode %d\n", retry_mode); |
| 1827 | |
| 1828 | if (retry_mode >= chip->read_retries) |
| 1829 | return -EINVAL; |
| 1830 | |
| 1831 | if (!chip->setup_read_retry) |
| 1832 | return -EOPNOTSUPP; |
| 1833 | |
| 1834 | return chip->setup_read_retry(mtd, retry_mode); |
| 1835 | } |
| 1836 | |
| 1837 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1838 | * nand_do_read_ops - [INTERN] Read data with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1839 | * @mtd: MTD device structure |
| 1840 | * @from: offset to read from |
| 1841 | * @ops: oob ops structure |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1842 | * |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1843 | * Internal function. Called with chip held. |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1844 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1845 | static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, |
| 1846 | struct mtd_oob_ops *ops) |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1847 | { |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 1848 | int chipnr, page, realpage, col, bytes, aligned, oob_required; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 1849 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1850 | int ret = 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1851 | uint32_t readlen = ops->len; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 1852 | uint32_t oobreadlen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 1853 | uint32_t max_oobsize = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 9aca334 | 2010-02-22 20:39:35 +0200 | [diff] [blame] | 1854 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1855 | uint8_t *bufpoi, *oob, *buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 1856 | int use_bufpoi; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 1857 | unsigned int max_bitflips = 0; |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 1858 | int retry_mode = 0; |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 1859 | bool ecc_fail = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1861 | chipnr = (int)(from >> chip->chip_shift); |
| 1862 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1864 | realpage = (int)(from >> chip->page_shift); |
| 1865 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1867 | col = (int)(from & (mtd->writesize - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1869 | buf = ops->datbuf; |
| 1870 | oob = ops->oobbuf; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 1871 | oob_required = oob ? 1 : 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1872 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 1873 | while (1) { |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 1874 | unsigned int ecc_failures = mtd->ecc_stats.failed; |
| 1875 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1876 | bytes = min(mtd->writesize - col, readlen); |
| 1877 | aligned = (bytes == mtd->writesize); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1878 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 1879 | if (!aligned) |
| 1880 | use_bufpoi = 1; |
| 1881 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 1882 | use_bufpoi = !virt_addr_valid(buf) || |
| 1883 | !IS_ALIGNED((unsigned long)buf, |
| 1884 | chip->buf_align); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 1885 | else |
| 1886 | use_bufpoi = 0; |
| 1887 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1888 | /* Is the current page in the buffer? */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1889 | if (realpage != chip->pagebuf || oob) { |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 1890 | bufpoi = use_bufpoi ? chip->buffers->databuf : buf; |
| 1891 | |
| 1892 | if (use_bufpoi && aligned) |
| 1893 | pr_debug("%s: using read bounce buffer for buf@%p\n", |
| 1894 | __func__, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 1896 | read_retry: |
Marc Gonzalez | 3371d66 | 2016-11-15 10:56:20 +0100 | [diff] [blame] | 1897 | if (nand_standard_page_accessors(&chip->ecc)) |
| 1898 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 1900 | /* |
| 1901 | * Now read the page into the buffer. Absent an error, |
| 1902 | * the read methods return max bitflips per ecc step. |
| 1903 | */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 1904 | if (unlikely(ops->mode == MTD_OPS_RAW)) |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1905 | ret = chip->ecc.read_page_raw(mtd, chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 1906 | oob_required, |
| 1907 | page); |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 1908 | else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) && |
| 1909 | !oob) |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1910 | ret = chip->ecc.read_subpage(mtd, chip, |
Huang Shijie | e004deb | 2014-01-03 11:01:40 +0800 | [diff] [blame] | 1911 | col, bytes, bufpoi, |
| 1912 | page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 1913 | else |
Sneha Narnakaje | 46a8cf2 | 2009-09-18 12:51:46 -0700 | [diff] [blame] | 1914 | ret = chip->ecc.read_page(mtd, chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 1915 | oob_required, page); |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 1916 | if (ret < 0) { |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 1917 | if (use_bufpoi) |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 1918 | /* Invalidate page cache */ |
| 1919 | chip->pagebuf = -1; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1920 | break; |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 1921 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1922 | |
| 1923 | /* Transfer not aligned data */ |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 1924 | if (use_bufpoi) { |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 1925 | if (!NAND_HAS_SUBPAGE_READ(chip) && !oob && |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 1926 | !(mtd->ecc_stats.failed - ecc_failures) && |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 1927 | (ops->mode != MTD_OPS_RAW)) { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1928 | chip->pagebuf = realpage; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 1929 | chip->pagebuf_bitflips = ret; |
| 1930 | } else { |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 1931 | /* Invalidate page cache */ |
| 1932 | chip->pagebuf = -1; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 1933 | } |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 1934 | memcpy(buf, chip->buffers->databuf + col, bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1936 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1937 | if (unlikely(oob)) { |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 1938 | int toread = min(oobreadlen, max_oobsize); |
| 1939 | |
| 1940 | if (toread) { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1941 | oob = nand_transfer_oob(mtd, |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 1942 | oob, ops, toread); |
| 1943 | oobreadlen -= toread; |
| 1944 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1945 | } |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 1946 | |
| 1947 | if (chip->options & NAND_NEED_READRDY) { |
| 1948 | /* Apply delay or wait for ready/busy pin */ |
| 1949 | if (!chip->dev_ready) |
| 1950 | udelay(chip->chip_delay); |
| 1951 | else |
| 1952 | nand_wait_ready(mtd); |
| 1953 | } |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 1954 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 1955 | if (mtd->ecc_stats.failed - ecc_failures) { |
Brian Norris | 28fa65e | 2014-02-12 16:08:28 -0800 | [diff] [blame] | 1956 | if (retry_mode + 1 < chip->read_retries) { |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 1957 | retry_mode++; |
| 1958 | ret = nand_setup_read_retry(mtd, |
| 1959 | retry_mode); |
| 1960 | if (ret < 0) |
| 1961 | break; |
| 1962 | |
| 1963 | /* Reset failures; retry */ |
| 1964 | mtd->ecc_stats.failed = ecc_failures; |
| 1965 | goto read_retry; |
| 1966 | } else { |
| 1967 | /* No more retry modes; real failure */ |
| 1968 | ecc_fail = true; |
| 1969 | } |
| 1970 | } |
| 1971 | |
| 1972 | buf += bytes; |
Masahiro Yamada | 0760468 | 2017-03-30 15:45:47 +0900 | [diff] [blame] | 1973 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1974 | } else { |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 1975 | memcpy(buf, chip->buffers->databuf + col, bytes); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1976 | buf += bytes; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 1977 | max_bitflips = max_t(unsigned int, max_bitflips, |
| 1978 | chip->pagebuf_bitflips); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1979 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1981 | readlen -= bytes; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1982 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 1983 | /* Reset to retry mode 0 */ |
| 1984 | if (retry_mode) { |
| 1985 | ret = nand_setup_read_retry(mtd, 0); |
| 1986 | if (ret < 0) |
| 1987 | break; |
| 1988 | retry_mode = 0; |
| 1989 | } |
| 1990 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1991 | if (!readlen) |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1992 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1994 | /* For subsequent reads align to page boundary */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | col = 0; |
| 1996 | /* Increment page address */ |
| 1997 | realpage++; |
| 1998 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1999 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | /* Check, if we cross a chip boundary */ |
| 2001 | if (!page) { |
| 2002 | chipnr++; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2003 | chip->select_chip(mtd, -1); |
| 2004 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | } |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2007 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2009 | ops->retlen = ops->len - (size_t) readlen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2010 | if (oob) |
| 2011 | ops->oobretlen = ops->ooblen - oobreadlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2013 | if (ret < 0) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2014 | return ret; |
| 2015 | |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 2016 | if (ecc_fail) |
Thomas Gleixner | 9a1fcdf | 2006-05-29 14:56:39 +0200 | [diff] [blame] | 2017 | return -EBADMSG; |
| 2018 | |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2019 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | /** |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2023 | * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2024 | * @mtd: MTD device structure |
| 2025 | * @from: offset to read from |
| 2026 | * @len: number of bytes to read |
| 2027 | * @retlen: pointer to variable to store the number of read bytes |
| 2028 | * @buf: the databuffer to put data |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2029 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2030 | * Get hold of the chip and call nand_do_read. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2031 | */ |
| 2032 | static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 2033 | size_t *retlen, uint8_t *buf) |
| 2034 | { |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2035 | struct mtd_oob_ops ops; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2036 | int ret; |
| 2037 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 2038 | nand_get_device(mtd, FL_READING); |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 2039 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2040 | ops.len = len; |
| 2041 | ops.datbuf = buf; |
Huang Shijie | 11041ae6 | 2012-07-03 16:44:14 +0800 | [diff] [blame] | 2042 | ops.mode = MTD_OPS_PLACE_OOB; |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2043 | ret = nand_do_read_ops(mtd, from, &ops); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2044 | *retlen = ops.retlen; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2045 | nand_release_device(mtd); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2046 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2050 | * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2051 | * @mtd: mtd info structure |
| 2052 | * @chip: nand chip info structure |
| 2053 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2054 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2055 | 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] | 2056 | { |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 2057 | chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2058 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 2059 | return 0; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2060 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2061 | EXPORT_SYMBOL(nand_read_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2062 | |
| 2063 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2064 | * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2065 | * with syndromes |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2066 | * @mtd: mtd info structure |
| 2067 | * @chip: nand chip info structure |
| 2068 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2069 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2070 | int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
| 2071 | int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2072 | { |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2073 | int length = mtd->oobsize; |
| 2074 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 2075 | int eccsize = chip->ecc.size; |
Baruch Siach | 2ea69d2 | 2015-01-22 15:23:05 +0200 | [diff] [blame] | 2076 | uint8_t *bufpoi = chip->oob_poi; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2077 | int i, toread, sndrnd = 0, pos; |
| 2078 | |
| 2079 | chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page); |
| 2080 | for (i = 0; i < chip->ecc.steps; i++) { |
| 2081 | if (sndrnd) { |
| 2082 | pos = eccsize + i * (eccsize + chunk); |
| 2083 | if (mtd->writesize > 512) |
| 2084 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1); |
| 2085 | else |
| 2086 | chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page); |
| 2087 | } else |
| 2088 | sndrnd = 1; |
| 2089 | toread = min_t(int, length, chunk); |
| 2090 | chip->read_buf(mtd, bufpoi, toread); |
| 2091 | bufpoi += toread; |
| 2092 | length -= toread; |
| 2093 | } |
| 2094 | if (length > 0) |
| 2095 | chip->read_buf(mtd, bufpoi, length); |
| 2096 | |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 2097 | return 0; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2098 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2099 | EXPORT_SYMBOL(nand_read_oob_syndrome); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2100 | |
| 2101 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2102 | * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2103 | * @mtd: mtd info structure |
| 2104 | * @chip: nand chip info structure |
| 2105 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2106 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2107 | 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] | 2108 | { |
| 2109 | int status = 0; |
| 2110 | const uint8_t *buf = chip->oob_poi; |
| 2111 | int length = mtd->oobsize; |
| 2112 | |
| 2113 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page); |
| 2114 | chip->write_buf(mtd, buf, length); |
| 2115 | /* Send command to program the OOB data */ |
| 2116 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 2117 | |
| 2118 | status = chip->waitfunc(mtd, chip); |
| 2119 | |
Savin Zlobec | 0d420f9 | 2006-06-21 11:51:20 +0200 | [diff] [blame] | 2120 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2121 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2122 | EXPORT_SYMBOL(nand_write_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2123 | |
| 2124 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2125 | * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2126 | * with syndrome - only for large page flash |
| 2127 | * @mtd: mtd info structure |
| 2128 | * @chip: nand chip info structure |
| 2129 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2130 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2131 | int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
| 2132 | int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2133 | { |
| 2134 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 2135 | int eccsize = chip->ecc.size, length = mtd->oobsize; |
| 2136 | int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps; |
| 2137 | const uint8_t *bufpoi = chip->oob_poi; |
| 2138 | |
| 2139 | /* |
| 2140 | * data-ecc-data-ecc ... ecc-oob |
| 2141 | * or |
| 2142 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob |
| 2143 | */ |
| 2144 | if (!chip->ecc.prepad && !chip->ecc.postpad) { |
| 2145 | pos = steps * (eccsize + chunk); |
| 2146 | steps = 0; |
| 2147 | } else |
Vitaly Wool | 8b0036e | 2006-07-11 09:11:25 +0200 | [diff] [blame] | 2148 | pos = eccsize; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2149 | |
| 2150 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page); |
| 2151 | for (i = 0; i < steps; i++) { |
| 2152 | if (sndcmd) { |
| 2153 | if (mtd->writesize <= 512) { |
| 2154 | uint32_t fill = 0xFFFFFFFF; |
| 2155 | |
| 2156 | len = eccsize; |
| 2157 | while (len > 0) { |
| 2158 | int num = min_t(int, len, 4); |
| 2159 | chip->write_buf(mtd, (uint8_t *)&fill, |
| 2160 | num); |
| 2161 | len -= num; |
| 2162 | } |
| 2163 | } else { |
| 2164 | pos = eccsize + i * (eccsize + chunk); |
| 2165 | chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1); |
| 2166 | } |
| 2167 | } else |
| 2168 | sndcmd = 1; |
| 2169 | len = min_t(int, length, chunk); |
| 2170 | chip->write_buf(mtd, bufpoi, len); |
| 2171 | bufpoi += len; |
| 2172 | length -= len; |
| 2173 | } |
| 2174 | if (length > 0) |
| 2175 | chip->write_buf(mtd, bufpoi, length); |
| 2176 | |
| 2177 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 2178 | status = chip->waitfunc(mtd, chip); |
| 2179 | |
| 2180 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
| 2181 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2182 | EXPORT_SYMBOL(nand_write_oob_syndrome); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2183 | |
| 2184 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2185 | * nand_do_read_oob - [INTERN] NAND read out-of-band |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2186 | * @mtd: MTD device structure |
| 2187 | * @from: offset to read from |
| 2188 | * @ops: oob operations description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2190 | * NAND read out-of-band data from the spare area. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2192 | static int nand_do_read_oob(struct mtd_info *mtd, loff_t from, |
| 2193 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | { |
Brian Norris | c00a099 | 2012-05-01 17:12:54 -0700 | [diff] [blame] | 2195 | int page, realpage, chipnr; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2196 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 2197 | struct mtd_ecc_stats stats; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2198 | int readlen = ops->ooblen; |
| 2199 | int len; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2200 | uint8_t *buf = ops->oobbuf; |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 2201 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2203 | pr_debug("%s: from = 0x%08Lx, len = %i\n", |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 2204 | __func__, (unsigned long long)from, readlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 2206 | stats = mtd->ecc_stats; |
| 2207 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 2208 | len = mtd_oobavail(mtd, ops); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2209 | |
| 2210 | if (unlikely(ops->ooboffs >= len)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2211 | pr_debug("%s: attempt to start read outside oob\n", |
| 2212 | __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2213 | return -EINVAL; |
| 2214 | } |
| 2215 | |
| 2216 | /* Do not allow reads past end of device */ |
| 2217 | if (unlikely(from >= mtd->size || |
| 2218 | ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) - |
| 2219 | (from >> chip->page_shift)) * len)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2220 | pr_debug("%s: attempt to read beyond end of device\n", |
| 2221 | __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2222 | return -EINVAL; |
| 2223 | } |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2224 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2225 | chipnr = (int)(from >> chip->chip_shift); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2226 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2228 | /* Shift to get page */ |
| 2229 | realpage = (int)(from >> chip->page_shift); |
| 2230 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2232 | while (1) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2233 | if (ops->mode == MTD_OPS_RAW) |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 2234 | ret = chip->ecc.read_oob_raw(mtd, chip, page); |
Brian Norris | c46f648 | 2011-08-30 18:45:38 -0700 | [diff] [blame] | 2235 | else |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 2236 | ret = chip->ecc.read_oob(mtd, chip, page); |
| 2237 | |
| 2238 | if (ret < 0) |
| 2239 | break; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2240 | |
| 2241 | len = min(len, readlen); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2242 | buf = nand_transfer_oob(mtd, buf, ops, len); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2243 | |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 2244 | if (chip->options & NAND_NEED_READRDY) { |
| 2245 | /* Apply delay or wait for ready/busy pin */ |
| 2246 | if (!chip->dev_ready) |
| 2247 | udelay(chip->chip_delay); |
| 2248 | else |
| 2249 | nand_wait_ready(mtd); |
| 2250 | } |
| 2251 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2252 | readlen -= len; |
Savin Zlobec | 0d420f9 | 2006-06-21 11:51:20 +0200 | [diff] [blame] | 2253 | if (!readlen) |
| 2254 | break; |
| 2255 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2256 | /* Increment page address */ |
| 2257 | realpage++; |
| 2258 | |
| 2259 | page = realpage & chip->pagemask; |
| 2260 | /* Check, if we cross a chip boundary */ |
| 2261 | if (!page) { |
| 2262 | chipnr++; |
| 2263 | chip->select_chip(mtd, -1); |
| 2264 | chip->select_chip(mtd, chipnr); |
| 2265 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | } |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2267 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 2269 | ops->oobretlen = ops->ooblen - readlen; |
| 2270 | |
| 2271 | if (ret < 0) |
| 2272 | return ret; |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 2273 | |
| 2274 | if (mtd->ecc_stats.failed - stats.failed) |
| 2275 | return -EBADMSG; |
| 2276 | |
| 2277 | return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | } |
| 2279 | |
| 2280 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2281 | * 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] | 2282 | * @mtd: MTD device structure |
| 2283 | * @from: offset to read from |
| 2284 | * @ops: oob operation description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2286 | * NAND read data and/or out-of-band data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2288 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
| 2289 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | { |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 2291 | int ret; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2292 | |
| 2293 | ops->retlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | |
| 2295 | /* Do not allow reads past end of device */ |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2296 | if (ops->datbuf && (from + ops->len) > mtd->size) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2297 | pr_debug("%s: attempt to read beyond end of device\n", |
| 2298 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | return -EINVAL; |
| 2300 | } |
| 2301 | |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 2302 | if (ops->mode != MTD_OPS_PLACE_OOB && |
| 2303 | ops->mode != MTD_OPS_AUTO_OOB && |
| 2304 | ops->mode != MTD_OPS_RAW) |
| 2305 | return -ENOTSUPP; |
| 2306 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 2307 | nand_get_device(mtd, FL_READING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2308 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2309 | if (!ops->datbuf) |
| 2310 | ret = nand_do_read_oob(mtd, from, ops); |
| 2311 | else |
| 2312 | ret = nand_do_read_ops(mtd, from, ops); |
| 2313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | nand_release_device(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2315 | return ret; |
| 2316 | } |
| 2317 | |
| 2318 | |
| 2319 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2320 | * nand_write_page_raw - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2321 | * @mtd: mtd info structure |
| 2322 | * @chip: nand chip info structure |
| 2323 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2324 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2325 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2326 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2327 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2328 | */ |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 2329 | int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
| 2330 | const uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2331 | { |
| 2332 | chip->write_buf(mtd, buf, mtd->writesize); |
Brian Norris | 279f08d | 2012-05-02 10:15:03 -0700 | [diff] [blame] | 2333 | if (oob_required) |
| 2334 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2335 | |
| 2336 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | } |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 2338 | EXPORT_SYMBOL(nand_write_page_raw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2340 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2341 | * nand_write_page_raw_syndrome - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2342 | * @mtd: mtd info structure |
| 2343 | * @chip: nand chip info structure |
| 2344 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2345 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2346 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2347 | * |
| 2348 | * We need a special oob layout and handling even when ECC isn't checked. |
| 2349 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2350 | static int nand_write_page_raw_syndrome(struct mtd_info *mtd, |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 2351 | struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2352 | const uint8_t *buf, int oob_required, |
| 2353 | int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2354 | { |
| 2355 | int eccsize = chip->ecc.size; |
| 2356 | int eccbytes = chip->ecc.bytes; |
| 2357 | uint8_t *oob = chip->oob_poi; |
| 2358 | int steps, size; |
| 2359 | |
| 2360 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
| 2361 | chip->write_buf(mtd, buf, eccsize); |
| 2362 | buf += eccsize; |
| 2363 | |
| 2364 | if (chip->ecc.prepad) { |
| 2365 | chip->write_buf(mtd, oob, chip->ecc.prepad); |
| 2366 | oob += chip->ecc.prepad; |
| 2367 | } |
| 2368 | |
Boris BREZILLON | 60c3bc1 | 2014-02-01 19:10:28 +0100 | [diff] [blame] | 2369 | chip->write_buf(mtd, oob, eccbytes); |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2370 | oob += eccbytes; |
| 2371 | |
| 2372 | if (chip->ecc.postpad) { |
| 2373 | chip->write_buf(mtd, oob, chip->ecc.postpad); |
| 2374 | oob += chip->ecc.postpad; |
| 2375 | } |
| 2376 | } |
| 2377 | |
| 2378 | size = mtd->oobsize - (oob - chip->oob_poi); |
| 2379 | if (size) |
| 2380 | chip->write_buf(mtd, oob, size); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2381 | |
| 2382 | return 0; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2383 | } |
| 2384 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2385 | * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2386 | * @mtd: mtd info structure |
| 2387 | * @chip: nand chip info structure |
| 2388 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2389 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2390 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2391 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2392 | 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] | 2393 | const uint8_t *buf, int oob_required, |
| 2394 | int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2395 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2396 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2397 | int eccbytes = chip->ecc.bytes; |
| 2398 | int eccsteps = chip->ecc.steps; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 2399 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2400 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2401 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2402 | /* Software ECC calculation */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2403 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 2404 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2405 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2406 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 2407 | chip->ecc.total); |
| 2408 | if (ret) |
| 2409 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2410 | |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2411 | return chip->ecc.write_page_raw(mtd, chip, buf, 1, page); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2415 | * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2416 | * @mtd: mtd info structure |
| 2417 | * @chip: nand chip info structure |
| 2418 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2419 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2420 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2421 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2422 | 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] | 2423 | const uint8_t *buf, int oob_required, |
| 2424 | int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2425 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2426 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2427 | int eccbytes = chip->ecc.bytes; |
| 2428 | int eccsteps = chip->ecc.steps; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 2429 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2430 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2431 | |
| 2432 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2433 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
David Woodhouse | 29da9ce | 2006-05-26 23:05:44 +0100 | [diff] [blame] | 2434 | chip->write_buf(mtd, p, eccsize); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2435 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 2436 | } |
| 2437 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2438 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 2439 | chip->ecc.total); |
| 2440 | if (ret) |
| 2441 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2442 | |
| 2443 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2444 | |
| 2445 | return 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2446 | } |
| 2447 | |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2448 | |
| 2449 | /** |
Brian Norris | 73c8aaf | 2015-02-28 02:04:18 -0800 | [diff] [blame] | 2450 | * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2451 | * @mtd: mtd info structure |
| 2452 | * @chip: nand chip info structure |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2453 | * @offset: column address of subpage within the page |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2454 | * @data_len: data length |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2455 | * @buf: data buffer |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2456 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2457 | * @page: page number to write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2458 | */ |
| 2459 | static int nand_write_subpage_hwecc(struct mtd_info *mtd, |
| 2460 | struct nand_chip *chip, uint32_t offset, |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2461 | uint32_t data_len, const uint8_t *buf, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2462 | int oob_required, int page) |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2463 | { |
| 2464 | uint8_t *oob_buf = chip->oob_poi; |
| 2465 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2466 | int ecc_size = chip->ecc.size; |
| 2467 | int ecc_bytes = chip->ecc.bytes; |
| 2468 | int ecc_steps = chip->ecc.steps; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2469 | uint32_t start_step = offset / ecc_size; |
| 2470 | uint32_t end_step = (offset + data_len - 1) / ecc_size; |
| 2471 | int oob_bytes = mtd->oobsize / ecc_steps; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2472 | int step, ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2473 | |
| 2474 | for (step = 0; step < ecc_steps; step++) { |
| 2475 | /* configure controller for WRITE access */ |
| 2476 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 2477 | |
| 2478 | /* write data (untouched subpages already masked by 0xFF) */ |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2479 | chip->write_buf(mtd, buf, ecc_size); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2480 | |
| 2481 | /* mask ECC of un-touched subpages by padding 0xFF */ |
| 2482 | if ((step < start_step) || (step > end_step)) |
| 2483 | memset(ecc_calc, 0xff, ecc_bytes); |
| 2484 | else |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2485 | chip->ecc.calculate(mtd, buf, ecc_calc); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2486 | |
| 2487 | /* mask OOB of un-touched subpages by padding 0xFF */ |
| 2488 | /* if oob_required, preserve OOB metadata of written subpage */ |
| 2489 | if (!oob_required || (step < start_step) || (step > end_step)) |
| 2490 | memset(oob_buf, 0xff, oob_bytes); |
| 2491 | |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2492 | buf += ecc_size; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2493 | ecc_calc += ecc_bytes; |
| 2494 | oob_buf += oob_bytes; |
| 2495 | } |
| 2496 | |
| 2497 | /* copy calculated ECC for whole page to chip->buffer->oob */ |
| 2498 | /* this include masked-value(0xFF) for unwritten subpages */ |
| 2499 | ecc_calc = chip->buffers->ecccalc; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2500 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 2501 | chip->ecc.total); |
| 2502 | if (ret) |
| 2503 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2504 | |
| 2505 | /* write OOB buffer to NAND device */ |
| 2506 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 2507 | |
| 2508 | return 0; |
| 2509 | } |
| 2510 | |
| 2511 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2512 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2513 | * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2514 | * @mtd: mtd info structure |
| 2515 | * @chip: nand chip info structure |
| 2516 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2517 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2518 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2519 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2520 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 2521 | * need a special oob layout and handling. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2522 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2523 | static int nand_write_page_syndrome(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2524 | struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2525 | const uint8_t *buf, int oob_required, |
| 2526 | int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2527 | { |
| 2528 | int i, eccsize = chip->ecc.size; |
| 2529 | int eccbytes = chip->ecc.bytes; |
| 2530 | int eccsteps = chip->ecc.steps; |
| 2531 | const uint8_t *p = buf; |
| 2532 | uint8_t *oob = chip->oob_poi; |
| 2533 | |
| 2534 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2535 | |
| 2536 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 2537 | chip->write_buf(mtd, p, eccsize); |
| 2538 | |
| 2539 | if (chip->ecc.prepad) { |
| 2540 | chip->write_buf(mtd, oob, chip->ecc.prepad); |
| 2541 | oob += chip->ecc.prepad; |
| 2542 | } |
| 2543 | |
| 2544 | chip->ecc.calculate(mtd, p, oob); |
| 2545 | chip->write_buf(mtd, oob, eccbytes); |
| 2546 | oob += eccbytes; |
| 2547 | |
| 2548 | if (chip->ecc.postpad) { |
| 2549 | chip->write_buf(mtd, oob, chip->ecc.postpad); |
| 2550 | oob += chip->ecc.postpad; |
| 2551 | } |
| 2552 | } |
| 2553 | |
| 2554 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 2555 | i = mtd->oobsize - (oob - chip->oob_poi); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2556 | if (i) |
| 2557 | chip->write_buf(mtd, oob, i); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2558 | |
| 2559 | return 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2560 | } |
| 2561 | |
| 2562 | /** |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 2563 | * nand_write_page - write one page |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2564 | * @mtd: MTD device structure |
| 2565 | * @chip: NAND chip descriptor |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2566 | * @offset: address offset within the page |
| 2567 | * @data_len: length of actual data to be written |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2568 | * @buf: the data to write |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2569 | * @oob_required: must write chip->oob_poi to OOB |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2570 | * @page: page number to write |
| 2571 | * @cached: cached programming |
| 2572 | * @raw: use _raw version of write_page |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2573 | */ |
| 2574 | 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] | 2575 | uint32_t offset, int data_len, const uint8_t *buf, |
Boris Brezillon | 0b4773fd | 2017-05-16 00:17:41 +0200 | [diff] [blame] | 2576 | int oob_required, int page, int raw) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2577 | { |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2578 | int status, subpage; |
| 2579 | |
| 2580 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && |
| 2581 | chip->ecc.write_subpage) |
| 2582 | subpage = offset || (data_len < mtd->writesize); |
| 2583 | else |
| 2584 | subpage = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2585 | |
Marc Gonzalez | 3371d66 | 2016-11-15 10:56:20 +0100 | [diff] [blame] | 2586 | if (nand_standard_page_accessors(&chip->ecc)) |
| 2587 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2588 | |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 2589 | if (unlikely(raw)) |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2590 | status = chip->ecc.write_page_raw(mtd, chip, buf, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2591 | oob_required, page); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2592 | else if (subpage) |
| 2593 | status = chip->ecc.write_subpage(mtd, chip, offset, data_len, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2594 | buf, oob_required, page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 2595 | else |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2596 | status = chip->ecc.write_page(mtd, chip, buf, oob_required, |
| 2597 | page); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2598 | |
| 2599 | if (status < 0) |
| 2600 | return status; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2601 | |
Boris Brezillon | 4114564 | 2017-05-16 18:27:49 +0200 | [diff] [blame] | 2602 | if (nand_standard_page_accessors(&chip->ecc)) { |
Boris Brezillon | 0b4773fd | 2017-05-16 00:17:41 +0200 | [diff] [blame] | 2603 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2604 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2605 | status = chip->waitfunc(mtd, chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2606 | if (status & NAND_STATUS_FAIL) |
| 2607 | return -EIO; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2608 | } |
| 2609 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2610 | return 0; |
| 2611 | } |
| 2612 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2613 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2614 | * nand_fill_oob - [INTERN] Transfer client buffer to oob |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2615 | * @mtd: MTD device structure |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2616 | * @oob: oob data buffer |
| 2617 | * @len: oob data write length |
| 2618 | * @ops: oob ops structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2619 | */ |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2620 | static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len, |
| 2621 | struct mtd_oob_ops *ops) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2622 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2623 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2624 | int ret; |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2625 | |
| 2626 | /* |
| 2627 | * Initialise to all 0xFF, to avoid the possibility of left over OOB |
| 2628 | * data from a previous OOB read. |
| 2629 | */ |
| 2630 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
| 2631 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2632 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2633 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2634 | case MTD_OPS_PLACE_OOB: |
| 2635 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2636 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); |
| 2637 | return oob + len; |
| 2638 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2639 | case MTD_OPS_AUTO_OOB: |
| 2640 | ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi, |
| 2641 | ops->ooboffs, len); |
| 2642 | BUG_ON(ret); |
| 2643 | return oob + len; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2644 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2645 | default: |
| 2646 | BUG(); |
| 2647 | } |
| 2648 | return NULL; |
| 2649 | } |
| 2650 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2651 | #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2652 | |
| 2653 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2654 | * nand_do_write_ops - [INTERN] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2655 | * @mtd: MTD device structure |
| 2656 | * @to: offset to write to |
| 2657 | * @ops: oob operations description structure |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2658 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2659 | * NAND write with ECC. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2660 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2661 | static int nand_do_write_ops(struct mtd_info *mtd, loff_t to, |
| 2662 | struct mtd_oob_ops *ops) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2663 | { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2664 | int chipnr, realpage, page, blockmask, column; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2665 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2666 | uint32_t writelen = ops->len; |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2667 | |
| 2668 | uint32_t oobwritelen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 2669 | uint32_t oobmaxlen = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2670 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2671 | uint8_t *oob = ops->oobbuf; |
| 2672 | uint8_t *buf = ops->datbuf; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2673 | int ret; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 2674 | int oob_required = oob ? 1 : 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2675 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2676 | ops->retlen = 0; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2677 | if (!writelen) |
| 2678 | return 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2679 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2680 | /* Reject writes, which are not page aligned */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2681 | if (NOTALIGNED(to) || NOTALIGNED(ops->len)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 2682 | pr_notice("%s: attempt to write non page aligned data\n", |
| 2683 | __func__); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2684 | return -EINVAL; |
| 2685 | } |
| 2686 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2687 | column = to & (mtd->writesize - 1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2688 | |
Thomas Gleixner | 6a93096 | 2006-06-28 00:11:45 +0200 | [diff] [blame] | 2689 | chipnr = (int)(to >> chip->chip_shift); |
| 2690 | chip->select_chip(mtd, chipnr); |
| 2691 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2692 | /* Check, if it is write protected */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2693 | if (nand_check_wp(mtd)) { |
| 2694 | ret = -EIO; |
| 2695 | goto err_out; |
| 2696 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2697 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2698 | realpage = (int)(to >> chip->page_shift); |
| 2699 | page = realpage & chip->pagemask; |
| 2700 | blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1; |
| 2701 | |
| 2702 | /* Invalidate the page cache, when we write to the cached page */ |
Brian Norris | 537ab1b | 2014-07-21 19:08:03 -0700 | [diff] [blame] | 2703 | if (to <= ((loff_t)chip->pagebuf << chip->page_shift) && |
| 2704 | ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len)) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2705 | chip->pagebuf = -1; |
| 2706 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2707 | /* Don't allow multipage oob writes with offset */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2708 | if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) { |
| 2709 | ret = -EINVAL; |
| 2710 | goto err_out; |
| 2711 | } |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2712 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2713 | while (1) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2714 | int bytes = mtd->writesize; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2715 | uint8_t *wbuf = buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2716 | int use_bufpoi; |
Hector Palacios | 144f4c9 | 2016-07-18 10:39:18 +0200 | [diff] [blame] | 2717 | int part_pagewr = (column || writelen < mtd->writesize); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2718 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2719 | if (part_pagewr) |
| 2720 | use_bufpoi = 1; |
| 2721 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 2722 | use_bufpoi = !virt_addr_valid(buf) || |
| 2723 | !IS_ALIGNED((unsigned long)buf, |
| 2724 | chip->buf_align); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2725 | else |
| 2726 | use_bufpoi = 0; |
| 2727 | |
| 2728 | /* Partial page write?, or need to use bounce buffer */ |
| 2729 | if (use_bufpoi) { |
| 2730 | pr_debug("%s: using write bounce buffer for buf@%p\n", |
| 2731 | __func__, buf); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2732 | if (part_pagewr) |
| 2733 | bytes = min_t(int, bytes - column, writelen); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2734 | chip->pagebuf = -1; |
| 2735 | memset(chip->buffers->databuf, 0xff, mtd->writesize); |
| 2736 | memcpy(&chip->buffers->databuf[column], buf, bytes); |
| 2737 | wbuf = chip->buffers->databuf; |
| 2738 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2739 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2740 | if (unlikely(oob)) { |
| 2741 | size_t len = min(oobwritelen, oobmaxlen); |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2742 | oob = nand_fill_oob(mtd, oob, len, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2743 | oobwritelen -= len; |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2744 | } else { |
| 2745 | /* We still need to erase leftover OOB data */ |
| 2746 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2747 | } |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 2748 | |
| 2749 | ret = nand_write_page(mtd, chip, column, bytes, wbuf, |
Boris Brezillon | 0b4773fd | 2017-05-16 00:17:41 +0200 | [diff] [blame] | 2750 | oob_required, page, |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 2751 | (ops->mode == MTD_OPS_RAW)); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2752 | if (ret) |
| 2753 | break; |
| 2754 | |
| 2755 | writelen -= bytes; |
| 2756 | if (!writelen) |
| 2757 | break; |
| 2758 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2759 | column = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2760 | buf += bytes; |
| 2761 | realpage++; |
| 2762 | |
| 2763 | page = realpage & chip->pagemask; |
| 2764 | /* Check, if we cross a chip boundary */ |
| 2765 | if (!page) { |
| 2766 | chipnr++; |
| 2767 | chip->select_chip(mtd, -1); |
| 2768 | chip->select_chip(mtd, chipnr); |
| 2769 | } |
| 2770 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2771 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2772 | ops->retlen = ops->len - writelen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2773 | if (unlikely(oob)) |
| 2774 | ops->oobretlen = ops->ooblen; |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2775 | |
| 2776 | err_out: |
| 2777 | chip->select_chip(mtd, -1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2778 | return ret; |
| 2779 | } |
| 2780 | |
| 2781 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2782 | * panic_nand_write - [MTD Interface] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2783 | * @mtd: MTD device structure |
| 2784 | * @to: offset to write to |
| 2785 | * @len: number of bytes to write |
| 2786 | * @retlen: pointer to variable to store the number of written bytes |
| 2787 | * @buf: the data to write |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2788 | * |
| 2789 | * NAND write with ECC. Used when performing writes in interrupt context, this |
| 2790 | * may for example be called by mtdoops when writing an oops while in panic. |
| 2791 | */ |
| 2792 | static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 2793 | size_t *retlen, const uint8_t *buf) |
| 2794 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2795 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2796 | struct mtd_oob_ops ops; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2797 | int ret; |
| 2798 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2799 | /* Wait for the device to get ready */ |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2800 | panic_nand_wait(mtd, chip, 400); |
| 2801 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2802 | /* Grab the device */ |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2803 | panic_nand_get_device(chip, mtd, FL_WRITING); |
| 2804 | |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 2805 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2806 | ops.len = len; |
| 2807 | ops.datbuf = (uint8_t *)buf; |
Huang Shijie | 11041ae6 | 2012-07-03 16:44:14 +0800 | [diff] [blame] | 2808 | ops.mode = MTD_OPS_PLACE_OOB; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2809 | |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2810 | ret = nand_do_write_ops(mtd, to, &ops); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2811 | |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2812 | *retlen = ops.retlen; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2813 | return ret; |
| 2814 | } |
| 2815 | |
| 2816 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2817 | * nand_write - [MTD Interface] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2818 | * @mtd: MTD device structure |
| 2819 | * @to: offset to write to |
| 2820 | * @len: number of bytes to write |
| 2821 | * @retlen: pointer to variable to store the number of written bytes |
| 2822 | * @buf: the data to write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2824 | * NAND write with ECC. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2826 | static int nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2827 | size_t *retlen, const uint8_t *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | { |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2829 | struct mtd_oob_ops ops; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2830 | int ret; |
| 2831 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 2832 | nand_get_device(mtd, FL_WRITING); |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 2833 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2834 | ops.len = len; |
| 2835 | ops.datbuf = (uint8_t *)buf; |
Huang Shijie | 11041ae6 | 2012-07-03 16:44:14 +0800 | [diff] [blame] | 2836 | ops.mode = MTD_OPS_PLACE_OOB; |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2837 | ret = nand_do_write_ops(mtd, to, &ops); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2838 | *retlen = ops.retlen; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2839 | nand_release_device(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2840 | return ret; |
| 2841 | } |
| 2842 | |
| 2843 | /** |
| 2844 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2845 | * @mtd: MTD device structure |
| 2846 | * @to: offset to write to |
| 2847 | * @ops: oob operation description structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2848 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2849 | * NAND write out-of-band. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2850 | */ |
| 2851 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 2852 | struct mtd_oob_ops *ops) |
| 2853 | { |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2854 | int chipnr, page, status, len; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2855 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2856 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2857 | pr_debug("%s: to = 0x%08x, len = %i\n", |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 2858 | __func__, (unsigned int)to, (int)ops->ooblen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2859 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 2860 | len = mtd_oobavail(mtd, ops); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2862 | /* Do not allow write past end of page */ |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2863 | if ((ops->ooboffs + ops->ooblen) > len) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2864 | pr_debug("%s: attempt to write past end of page\n", |
| 2865 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2866 | return -EINVAL; |
| 2867 | } |
| 2868 | |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2869 | if (unlikely(ops->ooboffs >= len)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2870 | pr_debug("%s: attempt to start write outside oob\n", |
| 2871 | __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2872 | return -EINVAL; |
| 2873 | } |
| 2874 | |
Jason Liu | 775adc3d4 | 2011-02-25 13:06:18 +0800 | [diff] [blame] | 2875 | /* Do not allow write past end of device */ |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2876 | if (unlikely(to >= mtd->size || |
| 2877 | ops->ooboffs + ops->ooblen > |
| 2878 | ((mtd->size >> chip->page_shift) - |
| 2879 | (to >> chip->page_shift)) * len)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2880 | pr_debug("%s: attempt to write beyond end of device\n", |
| 2881 | __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2882 | return -EINVAL; |
| 2883 | } |
| 2884 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2885 | chipnr = (int)(to >> chip->chip_shift); |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2886 | |
| 2887 | /* |
| 2888 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one |
| 2889 | * of my DiskOnChip 2000 test units) will clear the whole data page too |
| 2890 | * if we don't do this. I have no clue why, but I seem to have 'fixed' |
| 2891 | * it in the doc2000 driver in August 1999. dwmw2. |
| 2892 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2893 | nand_reset(chip, chipnr); |
| 2894 | |
| 2895 | chip->select_chip(mtd, chipnr); |
| 2896 | |
| 2897 | /* Shift to get page */ |
| 2898 | page = (int)(to >> chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2899 | |
| 2900 | /* Check, if it is write protected */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2901 | if (nand_check_wp(mtd)) { |
| 2902 | chip->select_chip(mtd, -1); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2903 | return -EROFS; |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2904 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2905 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2906 | /* Invalidate the page cache, if we write to the cached page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2907 | if (page == chip->pagebuf) |
| 2908 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2909 | |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2910 | nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops); |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 2911 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2912 | if (ops->mode == MTD_OPS_RAW) |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 2913 | status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask); |
| 2914 | else |
| 2915 | status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2916 | |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2917 | chip->select_chip(mtd, -1); |
| 2918 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2919 | if (status) |
| 2920 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2921 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2922 | ops->oobretlen = ops->ooblen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2924 | return 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2925 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2926 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2927 | /** |
| 2928 | * 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] | 2929 | * @mtd: MTD device structure |
| 2930 | * @to: offset to write to |
| 2931 | * @ops: oob operation description structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2932 | */ |
| 2933 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, |
| 2934 | struct mtd_oob_ops *ops) |
| 2935 | { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2936 | int ret = -ENOTSUPP; |
| 2937 | |
| 2938 | ops->retlen = 0; |
| 2939 | |
| 2940 | /* Do not allow writes past end of device */ |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2941 | if (ops->datbuf && (to + ops->len) > mtd->size) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2942 | pr_debug("%s: attempt to write beyond end of device\n", |
| 2943 | __func__); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2944 | return -EINVAL; |
| 2945 | } |
| 2946 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 2947 | nand_get_device(mtd, FL_WRITING); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2948 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2949 | switch (ops->mode) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2950 | case MTD_OPS_PLACE_OOB: |
| 2951 | case MTD_OPS_AUTO_OOB: |
| 2952 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2953 | break; |
| 2954 | |
| 2955 | default: |
| 2956 | goto out; |
| 2957 | } |
| 2958 | |
| 2959 | if (!ops->datbuf) |
| 2960 | ret = nand_do_write_oob(mtd, to, ops); |
| 2961 | else |
| 2962 | ret = nand_do_write_ops(mtd, to, ops); |
| 2963 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 2964 | out: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2965 | nand_release_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2966 | return ret; |
| 2967 | } |
| 2968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2969 | /** |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 2970 | * single_erase - [GENERIC] NAND standard block erase command function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2971 | * @mtd: MTD device structure |
| 2972 | * @page: the page address of the block which will be erased |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2973 | * |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 2974 | * Standard erase command for NAND chips. Returns NAND status. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | */ |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 2976 | static int single_erase(struct mtd_info *mtd, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2977 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2978 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2979 | /* Send commands to erase a block */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2980 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
| 2981 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 2982 | |
| 2983 | return chip->waitfunc(mtd, chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2984 | } |
| 2985 | |
| 2986 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2987 | * nand_erase - [MTD Interface] erase block(s) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2988 | * @mtd: MTD device structure |
| 2989 | * @instr: erase instruction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2990 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2991 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2992 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2993 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2994 | { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2995 | return nand_erase_nand(mtd, instr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2996 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2997 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2998 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2999 | * nand_erase_nand - [INTERN] erase block(s) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3000 | * @mtd: MTD device structure |
| 3001 | * @instr: erase instruction |
| 3002 | * @allowbbt: allow erasing the bbt area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3003 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3004 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3005 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3006 | int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, |
| 3007 | int allowbbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3008 | { |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 3009 | int page, status, pages_per_block, ret, chipnr; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3010 | struct nand_chip *chip = mtd_to_nand(mtd); |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 3011 | loff_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3012 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3013 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
| 3014 | __func__, (unsigned long long)instr->addr, |
| 3015 | (unsigned long long)instr->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 3017 | if (check_offs_len(mtd, instr->addr, instr->len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3018 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3020 | /* Grab the lock and see if the device is available */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3021 | nand_get_device(mtd, FL_ERASING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3022 | |
| 3023 | /* Shift to get first page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3024 | page = (int)(instr->addr >> chip->page_shift); |
| 3025 | chipnr = (int)(instr->addr >> chip->chip_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3026 | |
| 3027 | /* Calculate pages in each block */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3028 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3029 | |
| 3030 | /* Select the NAND device */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3031 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3032 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3033 | /* Check, if it is write protected */ |
| 3034 | if (nand_check_wp(mtd)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3035 | pr_debug("%s: device is write protected!\n", |
| 3036 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3037 | instr->state = MTD_ERASE_FAILED; |
| 3038 | goto erase_exit; |
| 3039 | } |
| 3040 | |
| 3041 | /* Loop through the pages */ |
| 3042 | len = instr->len; |
| 3043 | |
| 3044 | instr->state = MTD_ERASING; |
| 3045 | |
| 3046 | while (len) { |
Wolfram Sang | 12183a2 | 2011-12-21 23:01:20 +0100 | [diff] [blame] | 3047 | /* 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] | 3048 | if (nand_block_checkbad(mtd, ((loff_t) page) << |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 3049 | chip->page_shift, allowbbt)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 3050 | pr_warn("%s: attempt to erase a bad block at page 0x%08x\n", |
| 3051 | __func__, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3052 | instr->state = MTD_ERASE_FAILED; |
| 3053 | goto erase_exit; |
| 3054 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3055 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3056 | /* |
| 3057 | * Invalidate the page cache, if we erase the block which |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3058 | * contains the current cached page. |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3059 | */ |
| 3060 | if (page <= chip->pagebuf && chip->pagebuf < |
| 3061 | (page + pages_per_block)) |
| 3062 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3063 | |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 3064 | status = chip->erase(mtd, page & chip->pagemask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3065 | |
| 3066 | /* See if block erase succeeded */ |
David A. Marlin | a4ab4c5 | 2005-01-23 18:30:53 +0000 | [diff] [blame] | 3067 | if (status & NAND_STATUS_FAIL) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3068 | pr_debug("%s: failed erase, page 0x%08x\n", |
| 3069 | __func__, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3070 | instr->state = MTD_ERASE_FAILED; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 3071 | instr->fail_addr = |
| 3072 | ((loff_t)page << chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3073 | goto erase_exit; |
| 3074 | } |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 3075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3076 | /* Increment page address and decrement length */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 3077 | len -= (1ULL << chip->phys_erase_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | page += pages_per_block; |
| 3079 | |
| 3080 | /* Check, if we cross a chip boundary */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3081 | if (len && !(page & chip->pagemask)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3082 | chipnr++; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3083 | chip->select_chip(mtd, -1); |
| 3084 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | } |
| 3086 | } |
| 3087 | instr->state = MTD_ERASE_DONE; |
| 3088 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 3089 | erase_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3090 | |
| 3091 | ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | |
| 3093 | /* Deselect and wake up anyone waiting on the device */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3094 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3095 | nand_release_device(mtd); |
| 3096 | |
David Woodhouse | 49defc0 | 2007-10-06 15:01:59 -0400 | [diff] [blame] | 3097 | /* Do call back function */ |
| 3098 | if (!ret) |
| 3099 | mtd_erase_callback(instr); |
| 3100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3101 | /* Return more or less happy */ |
| 3102 | return ret; |
| 3103 | } |
| 3104 | |
| 3105 | /** |
| 3106 | * nand_sync - [MTD Interface] sync |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3107 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3108 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3109 | * Sync is actually a wait for chip ready function. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3110 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3111 | static void nand_sync(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3113 | pr_debug("%s: called\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3114 | |
| 3115 | /* Grab the lock and see if the device is available */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3116 | nand_get_device(mtd, FL_SYNCING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | /* Release it and go back */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3118 | nand_release_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3119 | } |
| 3120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3122 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3123 | * @mtd: MTD device structure |
| 3124 | * @offs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3126 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3127 | { |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 3128 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 3129 | int chipnr = (int)(offs >> chip->chip_shift); |
| 3130 | int ret; |
| 3131 | |
| 3132 | /* Select the NAND device */ |
| 3133 | nand_get_device(mtd, FL_READING); |
| 3134 | chip->select_chip(mtd, chipnr); |
| 3135 | |
| 3136 | ret = nand_block_checkbad(mtd, offs, 0); |
| 3137 | |
| 3138 | chip->select_chip(mtd, -1); |
| 3139 | nand_release_device(mtd); |
| 3140 | |
| 3141 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3142 | } |
| 3143 | |
| 3144 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3145 | * 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] | 3146 | * @mtd: MTD device structure |
| 3147 | * @ofs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3148 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3149 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3150 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3151 | int ret; |
| 3152 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3153 | ret = nand_block_isbad(mtd, ofs); |
| 3154 | if (ret) { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3155 | /* If it was bad already, return success and do nothing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3156 | if (ret > 0) |
| 3157 | return 0; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3158 | return ret; |
| 3159 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3160 | |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 3161 | return nand_block_markbad_lowlevel(mtd, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3162 | } |
| 3163 | |
| 3164 | /** |
Zach Brown | 5671842 | 2017-01-10 13:30:20 -0600 | [diff] [blame] | 3165 | * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd |
| 3166 | * @mtd: MTD device structure |
| 3167 | * @ofs: offset relative to mtd start |
| 3168 | * @len: length of mtd |
| 3169 | */ |
| 3170 | static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len) |
| 3171 | { |
| 3172 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 3173 | u32 part_start_block; |
| 3174 | u32 part_end_block; |
| 3175 | u32 part_start_die; |
| 3176 | u32 part_end_die; |
| 3177 | |
| 3178 | /* |
| 3179 | * max_bb_per_die and blocks_per_die used to determine |
| 3180 | * the maximum bad block count. |
| 3181 | */ |
| 3182 | if (!chip->max_bb_per_die || !chip->blocks_per_die) |
| 3183 | return -ENOTSUPP; |
| 3184 | |
| 3185 | /* Get the start and end of the partition in erase blocks. */ |
| 3186 | part_start_block = mtd_div_by_eb(ofs, mtd); |
| 3187 | part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1; |
| 3188 | |
| 3189 | /* Get the start and end LUNs of the partition. */ |
| 3190 | part_start_die = part_start_block / chip->blocks_per_die; |
| 3191 | part_end_die = part_end_block / chip->blocks_per_die; |
| 3192 | |
| 3193 | /* |
| 3194 | * Look up the bad blocks per unit and multiply by the number of units |
| 3195 | * that the partition spans. |
| 3196 | */ |
| 3197 | return chip->max_bb_per_die * (part_end_die - part_start_die + 1); |
| 3198 | } |
| 3199 | |
| 3200 | /** |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3201 | * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand |
| 3202 | * @mtd: MTD device structure |
| 3203 | * @chip: nand chip info structure |
| 3204 | * @addr: feature address. |
| 3205 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 3206 | */ |
| 3207 | static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 3208 | int addr, uint8_t *subfeature_param) |
| 3209 | { |
| 3210 | int status; |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 3211 | int i; |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3212 | |
David Mosberger | d914c93 | 2013-05-29 15:30:13 +0300 | [diff] [blame] | 3213 | if (!chip->onfi_version || |
| 3214 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 3215 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3216 | return -EINVAL; |
| 3217 | |
| 3218 | chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1); |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 3219 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 3220 | chip->write_byte(mtd, subfeature_param[i]); |
| 3221 | |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3222 | status = chip->waitfunc(mtd, chip); |
| 3223 | if (status & NAND_STATUS_FAIL) |
| 3224 | return -EIO; |
| 3225 | return 0; |
| 3226 | } |
| 3227 | |
| 3228 | /** |
| 3229 | * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand |
| 3230 | * @mtd: MTD device structure |
| 3231 | * @chip: nand chip info structure |
| 3232 | * @addr: feature address. |
| 3233 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 3234 | */ |
| 3235 | static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 3236 | int addr, uint8_t *subfeature_param) |
| 3237 | { |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 3238 | int i; |
| 3239 | |
David Mosberger | d914c93 | 2013-05-29 15:30:13 +0300 | [diff] [blame] | 3240 | if (!chip->onfi_version || |
| 3241 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 3242 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3243 | return -EINVAL; |
| 3244 | |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3245 | chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1); |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 3246 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 3247 | *subfeature_param++ = chip->read_byte(mtd); |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3248 | return 0; |
| 3249 | } |
| 3250 | |
| 3251 | /** |
Boris Brezillon | 4a78cc6 | 2017-05-26 17:10:15 +0200 | [diff] [blame] | 3252 | * nand_onfi_get_set_features_notsupp - set/get features stub returning |
| 3253 | * -ENOTSUPP |
| 3254 | * @mtd: MTD device structure |
| 3255 | * @chip: nand chip info structure |
| 3256 | * @addr: feature address. |
| 3257 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 3258 | * |
| 3259 | * Should be used by NAND controller drivers that do not support the SET/GET |
| 3260 | * FEATURES operations. |
| 3261 | */ |
| 3262 | int nand_onfi_get_set_features_notsupp(struct mtd_info *mtd, |
| 3263 | struct nand_chip *chip, int addr, |
| 3264 | u8 *subfeature_param) |
| 3265 | { |
| 3266 | return -ENOTSUPP; |
| 3267 | } |
| 3268 | EXPORT_SYMBOL(nand_onfi_get_set_features_notsupp); |
| 3269 | |
| 3270 | /** |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3271 | * nand_suspend - [MTD Interface] Suspend the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3272 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3273 | */ |
| 3274 | static int nand_suspend(struct mtd_info *mtd) |
| 3275 | { |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3276 | return nand_get_device(mtd, FL_PM_SUSPENDED); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3277 | } |
| 3278 | |
| 3279 | /** |
| 3280 | * nand_resume - [MTD Interface] Resume the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3281 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3282 | */ |
| 3283 | static void nand_resume(struct mtd_info *mtd) |
| 3284 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3285 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3286 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3287 | if (chip->state == FL_PM_SUSPENDED) |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3288 | nand_release_device(mtd); |
| 3289 | else |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 3290 | pr_err("%s called for a chip which is not in suspended state\n", |
| 3291 | __func__); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3292 | } |
| 3293 | |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 3294 | /** |
| 3295 | * nand_shutdown - [MTD Interface] Finish the current NAND operation and |
| 3296 | * prevent further operations |
| 3297 | * @mtd: MTD device structure |
| 3298 | */ |
| 3299 | static void nand_shutdown(struct mtd_info *mtd) |
| 3300 | { |
Brian Norris | 9ca641b | 2015-11-09 16:37:28 -0800 | [diff] [blame] | 3301 | nand_get_device(mtd, FL_PM_SUSPENDED); |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 3302 | } |
| 3303 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3304 | /* Set default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3305 | static void nand_set_defaults(struct nand_chip *chip) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3306 | { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3307 | unsigned int busw = chip->options & NAND_BUSWIDTH_16; |
| 3308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3309 | /* check for proper chip_delay setup, set 20us if not */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3310 | if (!chip->chip_delay) |
| 3311 | chip->chip_delay = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3312 | |
| 3313 | /* check, if a user supplied command function given */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3314 | if (chip->cmdfunc == NULL) |
| 3315 | chip->cmdfunc = nand_command; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3316 | |
| 3317 | /* check, if a user supplied wait function given */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3318 | if (chip->waitfunc == NULL) |
| 3319 | chip->waitfunc = nand_wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3321 | if (!chip->select_chip) |
| 3322 | chip->select_chip = nand_select_chip; |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 3323 | |
Huang Shijie | 4204ccc | 2013-08-16 10:10:07 +0800 | [diff] [blame] | 3324 | /* set for ONFI nand */ |
| 3325 | if (!chip->onfi_set_features) |
| 3326 | chip->onfi_set_features = nand_onfi_set_features; |
| 3327 | if (!chip->onfi_get_features) |
| 3328 | chip->onfi_get_features = nand_onfi_get_features; |
| 3329 | |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 3330 | /* If called twice, pointers that depend on busw may need to be reset */ |
| 3331 | if (!chip->read_byte || chip->read_byte == nand_read_byte) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3332 | chip->read_byte = busw ? nand_read_byte16 : nand_read_byte; |
| 3333 | if (!chip->read_word) |
| 3334 | chip->read_word = nand_read_word; |
| 3335 | if (!chip->block_bad) |
| 3336 | chip->block_bad = nand_block_bad; |
| 3337 | if (!chip->block_markbad) |
| 3338 | chip->block_markbad = nand_default_block_markbad; |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 3339 | if (!chip->write_buf || chip->write_buf == nand_write_buf) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3340 | chip->write_buf = busw ? nand_write_buf16 : nand_write_buf; |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 3341 | if (!chip->write_byte || chip->write_byte == nand_write_byte) |
| 3342 | chip->write_byte = busw ? nand_write_byte16 : nand_write_byte; |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 3343 | if (!chip->read_buf || chip->read_buf == nand_read_buf) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3344 | chip->read_buf = busw ? nand_read_buf16 : nand_read_buf; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3345 | if (!chip->scan_bbt) |
| 3346 | chip->scan_bbt = nand_default_bbt; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3347 | |
| 3348 | if (!chip->controller) { |
| 3349 | chip->controller = &chip->hwcontrol; |
Marc Gonzalez | d45bc58 | 2016-07-27 11:23:52 +0200 | [diff] [blame] | 3350 | nand_hw_control_init(chip->controller); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3351 | } |
| 3352 | |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 3353 | if (!chip->buf_align) |
| 3354 | chip->buf_align = 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3355 | } |
| 3356 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3357 | /* Sanitize ONFI strings so we can safely print them */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3358 | static void sanitize_string(uint8_t *s, size_t len) |
| 3359 | { |
| 3360 | ssize_t i; |
| 3361 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3362 | /* Null terminate */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3363 | s[len - 1] = 0; |
| 3364 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3365 | /* Remove non printable chars */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3366 | for (i = 0; i < len - 1; i++) { |
| 3367 | if (s[i] < ' ' || s[i] > 127) |
| 3368 | s[i] = '?'; |
| 3369 | } |
| 3370 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3371 | /* Remove trailing spaces */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3372 | strim(s); |
| 3373 | } |
| 3374 | |
| 3375 | static u16 onfi_crc16(u16 crc, u8 const *p, size_t len) |
| 3376 | { |
| 3377 | int i; |
| 3378 | while (len--) { |
| 3379 | crc ^= *p++ << 8; |
| 3380 | for (i = 0; i < 8; i++) |
| 3381 | crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0); |
| 3382 | } |
| 3383 | |
| 3384 | return crc; |
| 3385 | } |
| 3386 | |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3387 | /* Parse the Extended Parameter Page. */ |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3388 | static int nand_flash_detect_ext_param_page(struct nand_chip *chip, |
| 3389 | struct nand_onfi_params *p) |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3390 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3391 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3392 | struct onfi_ext_param_page *ep; |
| 3393 | struct onfi_ext_section *s; |
| 3394 | struct onfi_ext_ecc_info *ecc; |
| 3395 | uint8_t *cursor; |
| 3396 | int ret = -EINVAL; |
| 3397 | int len; |
| 3398 | int i; |
| 3399 | |
| 3400 | len = le16_to_cpu(p->ext_param_page_length) * 16; |
| 3401 | ep = kmalloc(len, GFP_KERNEL); |
Brian Norris | 5cb1327 | 2013-09-16 17:59:20 -0700 | [diff] [blame] | 3402 | if (!ep) |
| 3403 | return -ENOMEM; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3404 | |
| 3405 | /* Send our own NAND_CMD_PARAM. */ |
| 3406 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1); |
| 3407 | |
| 3408 | /* Use the Change Read Column command to skip the ONFI param pages. */ |
| 3409 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, |
| 3410 | sizeof(*p) * p->num_of_param_pages , -1); |
| 3411 | |
| 3412 | /* Read out the Extended Parameter Page. */ |
| 3413 | chip->read_buf(mtd, (uint8_t *)ep, len); |
| 3414 | if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2) |
| 3415 | != le16_to_cpu(ep->crc))) { |
| 3416 | pr_debug("fail in the CRC.\n"); |
| 3417 | goto ext_out; |
| 3418 | } |
| 3419 | |
| 3420 | /* |
| 3421 | * Check the signature. |
| 3422 | * Do not strictly follow the ONFI spec, maybe changed in future. |
| 3423 | */ |
| 3424 | if (strncmp(ep->sig, "EPPS", 4)) { |
| 3425 | pr_debug("The signature is invalid.\n"); |
| 3426 | goto ext_out; |
| 3427 | } |
| 3428 | |
| 3429 | /* find the ECC section. */ |
| 3430 | cursor = (uint8_t *)(ep + 1); |
| 3431 | for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) { |
| 3432 | s = ep->sections + i; |
| 3433 | if (s->type == ONFI_SECTION_TYPE_2) |
| 3434 | break; |
| 3435 | cursor += s->length * 16; |
| 3436 | } |
| 3437 | if (i == ONFI_EXT_SECTION_MAX) { |
| 3438 | pr_debug("We can not find the ECC section.\n"); |
| 3439 | goto ext_out; |
| 3440 | } |
| 3441 | |
| 3442 | /* get the info we want. */ |
| 3443 | ecc = (struct onfi_ext_ecc_info *)cursor; |
| 3444 | |
Brian Norris | 4ae7d22 | 2013-09-16 18:20:21 -0700 | [diff] [blame] | 3445 | if (!ecc->codeword_size) { |
| 3446 | pr_debug("Invalid codeword size\n"); |
| 3447 | goto ext_out; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3448 | } |
| 3449 | |
Brian Norris | 4ae7d22 | 2013-09-16 18:20:21 -0700 | [diff] [blame] | 3450 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 3451 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
Brian Norris | 5cb1327 | 2013-09-16 17:59:20 -0700 | [diff] [blame] | 3452 | ret = 0; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3453 | |
| 3454 | ext_out: |
| 3455 | kfree(ep); |
| 3456 | return ret; |
| 3457 | } |
| 3458 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3459 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3460 | * 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] | 3461 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3462 | static int nand_flash_detect_onfi(struct nand_chip *chip) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3463 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3464 | struct mtd_info *mtd = nand_to_mtd(chip); |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3465 | struct nand_onfi_params *p = &chip->onfi_params; |
Brian Norris | bd9c6e9 | 2013-11-29 22:04:28 -0800 | [diff] [blame] | 3466 | int i, j; |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3467 | int val; |
| 3468 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3469 | /* Try ONFI for unknown chip or LP */ |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3470 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1); |
| 3471 | if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' || |
| 3472 | chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I') |
| 3473 | return 0; |
| 3474 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3475 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1); |
| 3476 | for (i = 0; i < 3; i++) { |
Brian Norris | bd9c6e9 | 2013-11-29 22:04:28 -0800 | [diff] [blame] | 3477 | for (j = 0; j < sizeof(*p); j++) |
| 3478 | ((uint8_t *)p)[j] = chip->read_byte(mtd); |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3479 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) == |
| 3480 | le16_to_cpu(p->crc)) { |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3481 | break; |
| 3482 | } |
| 3483 | } |
| 3484 | |
Brian Norris | c7f23a7 | 2013-08-13 10:51:55 -0700 | [diff] [blame] | 3485 | if (i == 3) { |
| 3486 | pr_err("Could not find valid ONFI parameter page; aborting\n"); |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3487 | return 0; |
Brian Norris | c7f23a7 | 2013-08-13 10:51:55 -0700 | [diff] [blame] | 3488 | } |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3489 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3490 | /* Check version */ |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3491 | val = le16_to_cpu(p->revision); |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 3492 | if (val & (1 << 5)) |
| 3493 | chip->onfi_version = 23; |
| 3494 | else if (val & (1 << 4)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3495 | chip->onfi_version = 22; |
| 3496 | else if (val & (1 << 3)) |
| 3497 | chip->onfi_version = 21; |
| 3498 | else if (val & (1 << 2)) |
| 3499 | chip->onfi_version = 20; |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 3500 | else if (val & (1 << 1)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3501 | chip->onfi_version = 10; |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 3502 | |
| 3503 | if (!chip->onfi_version) { |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 3504 | pr_info("unsupported ONFI version: %d\n", val); |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 3505 | return 0; |
| 3506 | } |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3507 | |
| 3508 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 3509 | sanitize_string(p->model, sizeof(p->model)); |
| 3510 | if (!mtd->name) |
| 3511 | mtd->name = p->model; |
Brian Norris | 4355b70 | 2013-08-27 18:45:10 -0700 | [diff] [blame] | 3512 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3513 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
Brian Norris | 4355b70 | 2013-08-27 18:45:10 -0700 | [diff] [blame] | 3514 | |
| 3515 | /* |
| 3516 | * pages_per_block and blocks_per_lun may not be a power-of-2 size |
| 3517 | * (don't ask me who thought of this...). MTD assumes that these |
| 3518 | * dimensions will be power-of-2, so just truncate the remaining area. |
| 3519 | */ |
| 3520 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 3521 | mtd->erasesize *= mtd->writesize; |
| 3522 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3523 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
Brian Norris | 4355b70 | 2013-08-27 18:45:10 -0700 | [diff] [blame] | 3524 | |
| 3525 | /* See erasesize comment */ |
| 3526 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
Matthieu CASTET | 6379575 | 2012-03-19 15:35:25 +0100 | [diff] [blame] | 3527 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
Huang Shijie | 13fbd17 | 2013-09-25 14:58:13 +0800 | [diff] [blame] | 3528 | chip->bits_per_cell = p->bits_per_cell; |
Huang Shijie | e2985fc | 2013-05-17 11:17:30 +0800 | [diff] [blame] | 3529 | |
Zach Brown | 34da5f5 | 2017-01-10 13:30:21 -0600 | [diff] [blame] | 3530 | chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun); |
| 3531 | chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun); |
| 3532 | |
Huang Shijie | e2985fc | 2013-05-17 11:17:30 +0800 | [diff] [blame] | 3533 | if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS) |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3534 | chip->options |= NAND_BUSWIDTH_16; |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3535 | |
Huang Shijie | 10c86ba | 2013-05-17 11:17:26 +0800 | [diff] [blame] | 3536 | if (p->ecc_bits != 0xff) { |
| 3537 | chip->ecc_strength_ds = p->ecc_bits; |
| 3538 | chip->ecc_step_ds = 512; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3539 | } else if (chip->onfi_version >= 21 && |
| 3540 | (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) { |
| 3541 | |
| 3542 | /* |
| 3543 | * The nand_flash_detect_ext_param_page() uses the |
| 3544 | * Change Read Column command which maybe not supported |
| 3545 | * by the chip->cmdfunc. So try to update the chip->cmdfunc |
| 3546 | * now. We do not replace user supplied command function. |
| 3547 | */ |
| 3548 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 3549 | chip->cmdfunc = nand_command_lp; |
| 3550 | |
| 3551 | /* The Extended Parameter Page is supported since ONFI 2.1. */ |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3552 | if (nand_flash_detect_ext_param_page(chip, p)) |
Brian Norris | c7f23a7 | 2013-08-13 10:51:55 -0700 | [diff] [blame] | 3553 | pr_warn("Failed to detect ONFI extended param page\n"); |
| 3554 | } else { |
| 3555 | pr_warn("Could not retrieve ONFI ECC requirements\n"); |
Huang Shijie | 10c86ba | 2013-05-17 11:17:26 +0800 | [diff] [blame] | 3556 | } |
| 3557 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3558 | return 1; |
| 3559 | } |
| 3560 | |
| 3561 | /* |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 3562 | * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise. |
| 3563 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3564 | static int nand_flash_detect_jedec(struct nand_chip *chip) |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 3565 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3566 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 3567 | struct nand_jedec_params *p = &chip->jedec_params; |
| 3568 | struct jedec_ecc_info *ecc; |
| 3569 | int val; |
| 3570 | int i, j; |
| 3571 | |
| 3572 | /* Try JEDEC for unknown chip or LP */ |
| 3573 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1); |
| 3574 | if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' || |
| 3575 | chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' || |
| 3576 | chip->read_byte(mtd) != 'C') |
| 3577 | return 0; |
| 3578 | |
| 3579 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1); |
| 3580 | for (i = 0; i < 3; i++) { |
| 3581 | for (j = 0; j < sizeof(*p); j++) |
| 3582 | ((uint8_t *)p)[j] = chip->read_byte(mtd); |
| 3583 | |
| 3584 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) == |
| 3585 | le16_to_cpu(p->crc)) |
| 3586 | break; |
| 3587 | } |
| 3588 | |
| 3589 | if (i == 3) { |
| 3590 | pr_err("Could not find valid JEDEC parameter page; aborting\n"); |
| 3591 | return 0; |
| 3592 | } |
| 3593 | |
| 3594 | /* Check version */ |
| 3595 | val = le16_to_cpu(p->revision); |
| 3596 | if (val & (1 << 2)) |
| 3597 | chip->jedec_version = 10; |
| 3598 | else if (val & (1 << 1)) |
| 3599 | chip->jedec_version = 1; /* vendor specific version */ |
| 3600 | |
| 3601 | if (!chip->jedec_version) { |
| 3602 | pr_info("unsupported JEDEC version: %d\n", val); |
| 3603 | return 0; |
| 3604 | } |
| 3605 | |
| 3606 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 3607 | sanitize_string(p->model, sizeof(p->model)); |
| 3608 | if (!mtd->name) |
| 3609 | mtd->name = p->model; |
| 3610 | |
| 3611 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
| 3612 | |
| 3613 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 3614 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 3615 | mtd->erasesize *= mtd->writesize; |
| 3616 | |
| 3617 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
| 3618 | |
| 3619 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 3620 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
| 3621 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
| 3622 | chip->bits_per_cell = p->bits_per_cell; |
| 3623 | |
| 3624 | if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS) |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3625 | chip->options |= NAND_BUSWIDTH_16; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 3626 | |
| 3627 | /* ECC info */ |
| 3628 | ecc = &p->ecc_info[0]; |
| 3629 | |
| 3630 | if (ecc->codeword_size >= 9) { |
| 3631 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 3632 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
| 3633 | } else { |
| 3634 | pr_warn("Invalid codeword size\n"); |
| 3635 | } |
| 3636 | |
| 3637 | return 1; |
| 3638 | } |
| 3639 | |
| 3640 | /* |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 3641 | * nand_id_has_period - Check if an ID string has a given wraparound period |
| 3642 | * @id_data: the ID string |
| 3643 | * @arrlen: the length of the @id_data array |
| 3644 | * @period: the period of repitition |
| 3645 | * |
| 3646 | * Check if an ID string is repeated within a given sequence of bytes at |
| 3647 | * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a |
Brian Norris | d4d4f1b | 2012-11-14 21:54:20 -0800 | [diff] [blame] | 3648 | * 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] | 3649 | * if the repetition has a period of @period; otherwise, returns zero. |
| 3650 | */ |
| 3651 | static int nand_id_has_period(u8 *id_data, int arrlen, int period) |
| 3652 | { |
| 3653 | int i, j; |
| 3654 | for (i = 0; i < period; i++) |
| 3655 | for (j = i + period; j < arrlen; j += period) |
| 3656 | if (id_data[i] != id_data[j]) |
| 3657 | return 0; |
| 3658 | return 1; |
| 3659 | } |
| 3660 | |
| 3661 | /* |
| 3662 | * nand_id_len - Get the length of an ID string returned by CMD_READID |
| 3663 | * @id_data: the ID string |
| 3664 | * @arrlen: the length of the @id_data array |
| 3665 | |
| 3666 | * Returns the length of the ID string, according to known wraparound/trailing |
| 3667 | * zero patterns. If no pattern exists, returns the length of the array. |
| 3668 | */ |
| 3669 | static int nand_id_len(u8 *id_data, int arrlen) |
| 3670 | { |
| 3671 | int last_nonzero, period; |
| 3672 | |
| 3673 | /* Find last non-zero byte */ |
| 3674 | for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--) |
| 3675 | if (id_data[last_nonzero]) |
| 3676 | break; |
| 3677 | |
| 3678 | /* All zeros */ |
| 3679 | if (last_nonzero < 0) |
| 3680 | return 0; |
| 3681 | |
| 3682 | /* Calculate wraparound period */ |
| 3683 | for (period = 1; period < arrlen; period++) |
| 3684 | if (nand_id_has_period(id_data, arrlen, period)) |
| 3685 | break; |
| 3686 | |
| 3687 | /* There's a repeated pattern */ |
| 3688 | if (period < arrlen) |
| 3689 | return period; |
| 3690 | |
| 3691 | /* There are trailing zeros */ |
| 3692 | if (last_nonzero < arrlen - 1) |
| 3693 | return last_nonzero + 1; |
| 3694 | |
| 3695 | /* No pattern detected */ |
| 3696 | return arrlen; |
| 3697 | } |
| 3698 | |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 3699 | /* Extract the bits of per cell from the 3rd byte of the extended ID */ |
| 3700 | static int nand_get_bits_per_cell(u8 cellinfo) |
| 3701 | { |
| 3702 | int bits; |
| 3703 | |
| 3704 | bits = cellinfo & NAND_CI_CELLTYPE_MSK; |
| 3705 | bits >>= NAND_CI_CELLTYPE_SHIFT; |
| 3706 | return bits + 1; |
| 3707 | } |
| 3708 | |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 3709 | /* |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3710 | * Many new NAND share similar device ID codes, which represent the size of the |
| 3711 | * chip. The rest of the parameters must be decoded according to generic or |
| 3712 | * manufacturer-specific "extended ID" decoding patterns. |
| 3713 | */ |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 3714 | void nand_decode_ext_id(struct nand_chip *chip) |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3715 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3716 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 9b2d61f | 2016-06-08 10:34:57 +0200 | [diff] [blame] | 3717 | int extid; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3718 | u8 *id_data = chip->id.data; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3719 | /* The 3rd id byte holds MLC / multichip data */ |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 3720 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3721 | /* The 4th id byte is the important one */ |
| 3722 | extid = id_data[3]; |
| 3723 | |
Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 3724 | /* Calc pagesize */ |
| 3725 | mtd->writesize = 1024 << (extid & 0x03); |
| 3726 | extid >>= 2; |
| 3727 | /* Calc oobsize */ |
| 3728 | mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9); |
| 3729 | extid >>= 2; |
| 3730 | /* Calc blocksize. Blocksize is multiples of 64KiB */ |
| 3731 | mtd->erasesize = (64 * 1024) << (extid & 0x03); |
| 3732 | extid >>= 2; |
| 3733 | /* Get buswidth information */ |
| 3734 | if (extid & 0x1) |
| 3735 | chip->options |= NAND_BUSWIDTH_16; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3736 | } |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 3737 | EXPORT_SYMBOL_GPL(nand_decode_ext_id); |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3738 | |
| 3739 | /* |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 3740 | * Old devices have chip data hardcoded in the device ID table. nand_decode_id |
| 3741 | * decodes a matching ID table entry and assigns the MTD size parameters for |
| 3742 | * the chip. |
| 3743 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3744 | 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] | 3745 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3746 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 3747 | |
| 3748 | mtd->erasesize = type->erasesize; |
| 3749 | mtd->writesize = type->pagesize; |
| 3750 | mtd->oobsize = mtd->writesize / 32; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 3751 | |
Huang Shijie | 1c195e9 | 2013-09-25 14:58:12 +0800 | [diff] [blame] | 3752 | /* All legacy ID NAND are small-page, SLC */ |
| 3753 | chip->bits_per_cell = 1; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 3754 | } |
| 3755 | |
| 3756 | /* |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 3757 | * Set the bad block marker/indicator (BBM/BBI) patterns according to some |
| 3758 | * heuristic patterns using various detected parameters (e.g., manufacturer, |
| 3759 | * page size, cell-type information). |
| 3760 | */ |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3761 | static void nand_decode_bbm_options(struct nand_chip *chip) |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 3762 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3763 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 3764 | |
| 3765 | /* Set the bad block position */ |
| 3766 | if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16)) |
| 3767 | chip->badblockpos = NAND_LARGE_BADBLOCK_POS; |
| 3768 | else |
| 3769 | chip->badblockpos = NAND_SMALL_BADBLOCK_POS; |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 3770 | } |
| 3771 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3772 | static inline bool is_full_id_nand(struct nand_flash_dev *type) |
| 3773 | { |
| 3774 | return type->id_len; |
| 3775 | } |
| 3776 | |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3777 | static bool find_full_id_nand(struct nand_chip *chip, |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3778 | struct nand_flash_dev *type) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3779 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3780 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3781 | u8 *id_data = chip->id.data; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3782 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3783 | if (!strncmp(type->id, id_data, type->id_len)) { |
| 3784 | mtd->writesize = type->pagesize; |
| 3785 | mtd->erasesize = type->erasesize; |
| 3786 | mtd->oobsize = type->oobsize; |
| 3787 | |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 3788 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3789 | chip->chipsize = (uint64_t)type->chipsize << 20; |
| 3790 | chip->options |= type->options; |
Huang Shijie | 5721934 | 2013-05-17 11:17:32 +0800 | [diff] [blame] | 3791 | chip->ecc_strength_ds = NAND_ECC_STRENGTH(type); |
| 3792 | chip->ecc_step_ds = NAND_ECC_STEP(type); |
Boris BREZILLON | 57a94e2 | 2014-09-22 20:11:50 +0200 | [diff] [blame] | 3793 | chip->onfi_timing_mode_default = |
| 3794 | type->onfi_timing_mode_default; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3795 | |
Cai Zhiyong | 092b6a1 | 2013-12-25 21:19:21 +0800 | [diff] [blame] | 3796 | if (!mtd->name) |
| 3797 | mtd->name = type->name; |
| 3798 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3799 | return true; |
| 3800 | } |
| 3801 | return false; |
| 3802 | } |
| 3803 | |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 3804 | /* |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 3805 | * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC |
| 3806 | * compliant and does not have a full-id or legacy-id entry in the nand_ids |
| 3807 | * table. |
| 3808 | */ |
| 3809 | static void nand_manufacturer_detect(struct nand_chip *chip) |
| 3810 | { |
| 3811 | /* |
| 3812 | * Try manufacturer detection if available and use |
| 3813 | * nand_decode_ext_id() otherwise. |
| 3814 | */ |
| 3815 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame^] | 3816 | chip->manufacturer.desc->ops->detect) { |
| 3817 | /* The 3rd id byte holds MLC / multichip data */ |
| 3818 | 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] | 3819 | chip->manufacturer.desc->ops->detect(chip); |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame^] | 3820 | } else { |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 3821 | nand_decode_ext_id(chip); |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame^] | 3822 | } |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 3823 | } |
| 3824 | |
| 3825 | /* |
| 3826 | * Manufacturer initialization. This function is called for all NANDs including |
| 3827 | * ONFI and JEDEC compliant ones. |
| 3828 | * Manufacturer drivers should put all their specific initialization code in |
| 3829 | * their ->init() hook. |
| 3830 | */ |
| 3831 | static int nand_manufacturer_init(struct nand_chip *chip) |
| 3832 | { |
| 3833 | if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops || |
| 3834 | !chip->manufacturer.desc->ops->init) |
| 3835 | return 0; |
| 3836 | |
| 3837 | return chip->manufacturer.desc->ops->init(chip); |
| 3838 | } |
| 3839 | |
| 3840 | /* |
| 3841 | * Manufacturer cleanup. This function is called for all NANDs including |
| 3842 | * ONFI and JEDEC compliant ones. |
| 3843 | * Manufacturer drivers should put all their specific cleanup code in their |
| 3844 | * ->cleanup() hook. |
| 3845 | */ |
| 3846 | static void nand_manufacturer_cleanup(struct nand_chip *chip) |
| 3847 | { |
| 3848 | /* Release manufacturer private data */ |
| 3849 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
| 3850 | chip->manufacturer.desc->ops->cleanup) |
| 3851 | chip->manufacturer.desc->ops->cleanup(chip); |
| 3852 | } |
| 3853 | |
| 3854 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3855 | * 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] | 3856 | */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame] | 3857 | 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] | 3858 | { |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 3859 | const struct nand_manufacturer *manufacturer; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3860 | struct mtd_info *mtd = nand_to_mtd(chip); |
Cai Zhiyong | bb77082 | 2013-12-25 20:11:15 +0800 | [diff] [blame] | 3861 | int busw; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 3862 | int i; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3863 | u8 *id_data = chip->id.data; |
| 3864 | u8 maf_id, dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3865 | |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 3866 | /* |
| 3867 | * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3868 | * after power-up. |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 3869 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 3870 | nand_reset(chip, 0); |
| 3871 | |
| 3872 | /* Select the device */ |
| 3873 | chip->select_chip(mtd, 0); |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 3874 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3875 | /* Send the command for reading device ID */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3876 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3877 | |
| 3878 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3879 | maf_id = chip->read_byte(mtd); |
| 3880 | dev_id = chip->read_byte(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3881 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3882 | /* |
| 3883 | * 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] | 3884 | * interface concerns can cause random data which looks like a |
| 3885 | * possibly credible NAND flash to appear. If the two results do |
| 3886 | * not match, ignore the device completely. |
| 3887 | */ |
| 3888 | |
| 3889 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
| 3890 | |
Brian Norris | 4aef9b7 | 2012-09-24 20:40:48 -0700 | [diff] [blame] | 3891 | /* Read entire ID string */ |
Jean-Louis Thekekara | 5158bd5 | 2017-06-29 19:08:30 +0200 | [diff] [blame] | 3892 | for (i = 0; i < ARRAY_SIZE(chip->id.data); i++) |
Kevin Cernekee | 426c457 | 2010-05-04 20:58:03 -0700 | [diff] [blame] | 3893 | id_data[i] = chip->read_byte(mtd); |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 3894 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3895 | if (id_data[0] != maf_id || id_data[1] != dev_id) { |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 3896 | 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] | 3897 | maf_id, dev_id, id_data[0], id_data[1]); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 3898 | return -ENODEV; |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 3899 | } |
| 3900 | |
Jean-Louis Thekekara | 5158bd5 | 2017-06-29 19:08:30 +0200 | [diff] [blame] | 3901 | 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] | 3902 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 3903 | /* Try to identify manufacturer */ |
| 3904 | manufacturer = nand_get_manufacturer(maf_id); |
| 3905 | chip->manufacturer.desc = manufacturer; |
| 3906 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3907 | if (!type) |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 3908 | type = nand_flash_ids; |
| 3909 | |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3910 | /* |
| 3911 | * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic |
| 3912 | * override it. |
| 3913 | * This is required to make sure initial NAND bus width set by the |
| 3914 | * NAND controller driver is coherent with the real NAND bus width |
| 3915 | * (extracted by auto-detection code). |
| 3916 | */ |
| 3917 | busw = chip->options & NAND_BUSWIDTH_16; |
| 3918 | |
| 3919 | /* |
| 3920 | * The flag is only set (never cleared), reset it to its default value |
| 3921 | * before starting auto-detection. |
| 3922 | */ |
| 3923 | chip->options &= ~NAND_BUSWIDTH_16; |
| 3924 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3925 | for (; type->name != NULL; type++) { |
| 3926 | if (is_full_id_nand(type)) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3927 | if (find_full_id_nand(chip, type)) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3928 | goto ident_done; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3929 | } else if (dev_id == type->dev_id) { |
Brian Norris | db5b09f | 2015-05-22 10:43:12 -0700 | [diff] [blame] | 3930 | break; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 3931 | } |
| 3932 | } |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 3933 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3934 | chip->onfi_version = 0; |
| 3935 | if (!type->name || !type->pagesize) { |
Masahiro Yamada | 35fc519 | 2014-04-09 16:26:26 +0900 | [diff] [blame] | 3936 | /* Check if the chip is ONFI compliant */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3937 | if (nand_flash_detect_onfi(chip)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3938 | goto ident_done; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 3939 | |
| 3940 | /* Check if the chip is JEDEC compliant */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3941 | if (nand_flash_detect_jedec(chip)) |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 3942 | goto ident_done; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3943 | } |
| 3944 | |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 3945 | if (!type->name) |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 3946 | return -ENODEV; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3947 | |
Thomas Gleixner | ba0251fe | 2006-05-27 01:02:13 +0200 | [diff] [blame] | 3948 | if (!mtd->name) |
| 3949 | mtd->name = type->name; |
| 3950 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 3951 | chip->chipsize = (uint64_t)type->chipsize << 20; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3952 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 3953 | if (!type->pagesize) |
| 3954 | nand_manufacturer_detect(chip); |
| 3955 | else |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3956 | nand_decode_id(chip, type); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 3957 | |
Brian Norris | bf7a01b | 2012-07-13 09:28:24 -0700 | [diff] [blame] | 3958 | /* Get chip options */ |
| 3959 | chip->options |= type->options; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3960 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3961 | ident_done: |
| 3962 | |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 3963 | if (chip->options & NAND_BUSWIDTH_AUTO) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3964 | WARN_ON(busw & NAND_BUSWIDTH_16); |
| 3965 | nand_set_defaults(chip); |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 3966 | } else if (busw != (chip->options & NAND_BUSWIDTH_16)) { |
| 3967 | /* |
| 3968 | * Check, if buswidth is correct. Hardware drivers should set |
| 3969 | * chip correct! |
| 3970 | */ |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 3971 | 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] | 3972 | maf_id, dev_id); |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 3973 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 3974 | mtd->name); |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3975 | pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8, |
| 3976 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 3977 | return -EINVAL; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3978 | } |
| 3979 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3980 | nand_decode_bbm_options(chip); |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 3981 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3982 | /* Calculate the address shift from the page size */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3983 | chip->page_shift = ffs(mtd->writesize) - 1; |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3984 | /* Convert chipsize to number of pages per chip -1 */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3985 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3986 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3987 | chip->bbt_erase_shift = chip->phys_erase_shift = |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3988 | ffs(mtd->erasesize) - 1; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 3989 | if (chip->chipsize & 0xffffffff) |
| 3990 | chip->chip_shift = ffs((unsigned)chip->chipsize) - 1; |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 3991 | else { |
| 3992 | chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)); |
| 3993 | chip->chip_shift += 32 - 1; |
| 3994 | } |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3995 | |
Artem Bityutskiy | 26d9be1 | 2011-04-28 20:26:59 +0300 | [diff] [blame] | 3996 | chip->badblockbits = 8; |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 3997 | chip->erase = single_erase; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3998 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3999 | /* Do not replace user supplied command function! */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4000 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 4001 | chip->cmdfunc = nand_command_lp; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4002 | |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4003 | 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] | 4004 | maf_id, dev_id); |
Huang Shijie | ffdac6cd | 2014-02-21 13:39:41 +0800 | [diff] [blame] | 4005 | |
| 4006 | if (chip->onfi_version) |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4007 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4008 | chip->onfi_params.model); |
Huang Shijie | ffdac6cd | 2014-02-21 13:39:41 +0800 | [diff] [blame] | 4009 | else if (chip->jedec_version) |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4010 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4011 | chip->jedec_params.model); |
Huang Shijie | ffdac6cd | 2014-02-21 13:39:41 +0800 | [diff] [blame] | 4012 | else |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 4013 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 4014 | type->name); |
Huang Shijie | ffdac6cd | 2014-02-21 13:39:41 +0800 | [diff] [blame] | 4015 | |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 4016 | 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] | 4017 | (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC", |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 4018 | mtd->erasesize >> 10, mtd->writesize, mtd->oobsize); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4019 | return 0; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4020 | } |
| 4021 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4022 | static const char * const nand_ecc_modes[] = { |
| 4023 | [NAND_ECC_NONE] = "none", |
| 4024 | [NAND_ECC_SOFT] = "soft", |
| 4025 | [NAND_ECC_HW] = "hw", |
| 4026 | [NAND_ECC_HW_SYNDROME] = "hw_syndrome", |
| 4027 | [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first", |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 4028 | [NAND_ECC_ON_DIE] = "on-die", |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4029 | }; |
| 4030 | |
| 4031 | static int of_get_nand_ecc_mode(struct device_node *np) |
| 4032 | { |
| 4033 | const char *pm; |
| 4034 | int err, i; |
| 4035 | |
| 4036 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 4037 | if (err < 0) |
| 4038 | return err; |
| 4039 | |
| 4040 | for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++) |
| 4041 | if (!strcasecmp(pm, nand_ecc_modes[i])) |
| 4042 | return i; |
| 4043 | |
Rafał Miłecki | ae211bc | 2016-04-17 22:53:06 +0200 | [diff] [blame] | 4044 | /* |
| 4045 | * For backward compatibility we support few obsoleted values that don't |
| 4046 | * have their mappings into nand_ecc_modes_t anymore (they were merged |
| 4047 | * with other enums). |
| 4048 | */ |
| 4049 | if (!strcasecmp(pm, "soft_bch")) |
| 4050 | return NAND_ECC_SOFT; |
| 4051 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4052 | return -ENODEV; |
| 4053 | } |
| 4054 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4055 | static const char * const nand_ecc_algos[] = { |
| 4056 | [NAND_ECC_HAMMING] = "hamming", |
| 4057 | [NAND_ECC_BCH] = "bch", |
| 4058 | }; |
| 4059 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4060 | static int of_get_nand_ecc_algo(struct device_node *np) |
| 4061 | { |
| 4062 | const char *pm; |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4063 | int err, i; |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4064 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4065 | err = of_property_read_string(np, "nand-ecc-algo", &pm); |
| 4066 | if (!err) { |
| 4067 | for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++) |
| 4068 | if (!strcasecmp(pm, nand_ecc_algos[i])) |
| 4069 | return i; |
| 4070 | return -ENODEV; |
| 4071 | } |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4072 | |
| 4073 | /* |
| 4074 | * For backward compatibility we also read "nand-ecc-mode" checking |
| 4075 | * for some obsoleted values that were specifying ECC algorithm. |
| 4076 | */ |
| 4077 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 4078 | if (err < 0) |
| 4079 | return err; |
| 4080 | |
| 4081 | if (!strcasecmp(pm, "soft")) |
| 4082 | return NAND_ECC_HAMMING; |
| 4083 | else if (!strcasecmp(pm, "soft_bch")) |
| 4084 | return NAND_ECC_BCH; |
| 4085 | |
| 4086 | return -ENODEV; |
| 4087 | } |
| 4088 | |
| 4089 | static int of_get_nand_ecc_step_size(struct device_node *np) |
| 4090 | { |
| 4091 | int ret; |
| 4092 | u32 val; |
| 4093 | |
| 4094 | ret = of_property_read_u32(np, "nand-ecc-step-size", &val); |
| 4095 | return ret ? ret : val; |
| 4096 | } |
| 4097 | |
| 4098 | static int of_get_nand_ecc_strength(struct device_node *np) |
| 4099 | { |
| 4100 | int ret; |
| 4101 | u32 val; |
| 4102 | |
| 4103 | ret = of_property_read_u32(np, "nand-ecc-strength", &val); |
| 4104 | return ret ? ret : val; |
| 4105 | } |
| 4106 | |
| 4107 | static int of_get_nand_bus_width(struct device_node *np) |
| 4108 | { |
| 4109 | u32 val; |
| 4110 | |
| 4111 | if (of_property_read_u32(np, "nand-bus-width", &val)) |
| 4112 | return 8; |
| 4113 | |
| 4114 | switch (val) { |
| 4115 | case 8: |
| 4116 | case 16: |
| 4117 | return val; |
| 4118 | default: |
| 4119 | return -EIO; |
| 4120 | } |
| 4121 | } |
| 4122 | |
| 4123 | static bool of_get_nand_on_flash_bbt(struct device_node *np) |
| 4124 | { |
| 4125 | return of_property_read_bool(np, "nand-on-flash-bbt"); |
| 4126 | } |
| 4127 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4128 | static int nand_dt_init(struct nand_chip *chip) |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4129 | { |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4130 | struct device_node *dn = nand_get_flash_node(chip); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4131 | int ecc_mode, ecc_algo, ecc_strength, ecc_step; |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4132 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4133 | if (!dn) |
| 4134 | return 0; |
| 4135 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4136 | if (of_get_nand_bus_width(dn) == 16) |
| 4137 | chip->options |= NAND_BUSWIDTH_16; |
| 4138 | |
| 4139 | if (of_get_nand_on_flash_bbt(dn)) |
| 4140 | chip->bbt_options |= NAND_BBT_USE_FLASH; |
| 4141 | |
| 4142 | ecc_mode = of_get_nand_ecc_mode(dn); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4143 | ecc_algo = of_get_nand_ecc_algo(dn); |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4144 | ecc_strength = of_get_nand_ecc_strength(dn); |
| 4145 | ecc_step = of_get_nand_ecc_step_size(dn); |
| 4146 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4147 | if (ecc_mode >= 0) |
| 4148 | chip->ecc.mode = ecc_mode; |
| 4149 | |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4150 | if (ecc_algo >= 0) |
| 4151 | chip->ecc.algo = ecc_algo; |
| 4152 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4153 | if (ecc_strength >= 0) |
| 4154 | chip->ecc.strength = ecc_strength; |
| 4155 | |
| 4156 | if (ecc_step > 0) |
| 4157 | chip->ecc.size = ecc_step; |
| 4158 | |
Boris Brezillon | ba78ee0 | 2016-06-08 17:04:22 +0200 | [diff] [blame] | 4159 | if (of_property_read_bool(dn, "nand-ecc-maximize")) |
| 4160 | chip->ecc.options |= NAND_ECC_MAXIMIZE; |
| 4161 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4162 | return 0; |
| 4163 | } |
| 4164 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4165 | /** |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4166 | * nand_scan_ident - [NAND Interface] Scan for the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4167 | * @mtd: MTD device structure |
| 4168 | * @maxchips: number of chips to scan for |
| 4169 | * @table: alternative NAND ID table |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4170 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4171 | * This is the first phase of the normal nand_scan() function. It reads the |
| 4172 | * flash ID and sets up MTD fields accordingly. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4173 | * |
| 4174 | */ |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4175 | int nand_scan_ident(struct mtd_info *mtd, int maxchips, |
| 4176 | struct nand_flash_dev *table) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4177 | { |
Cai Zhiyong | bb77082 | 2013-12-25 20:11:15 +0800 | [diff] [blame] | 4178 | int i, nand_maf_id, nand_dev_id; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4179 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4180 | int ret; |
| 4181 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4182 | ret = nand_dt_init(chip); |
| 4183 | if (ret) |
| 4184 | return ret; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4185 | |
Brian Norris | f7a8e38 | 2016-01-05 10:39:45 -0800 | [diff] [blame] | 4186 | if (!mtd->name && mtd->dev.parent) |
| 4187 | mtd->name = dev_name(mtd->dev.parent); |
| 4188 | |
Andrey Smirnov | 76fe334 | 2016-07-21 14:59:20 -0700 | [diff] [blame] | 4189 | if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) { |
| 4190 | /* |
| 4191 | * Default functions assigned for chip_select() and |
| 4192 | * cmdfunc() both expect cmd_ctrl() to be populated, |
| 4193 | * so we need to check that that's the case |
| 4194 | */ |
| 4195 | pr_err("chip.cmd_ctrl() callback is not provided"); |
| 4196 | return -EINVAL; |
| 4197 | } |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4198 | /* Set the default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4199 | nand_set_defaults(chip); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4200 | |
| 4201 | /* Read the flash type */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame] | 4202 | ret = nand_detect(chip, table); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4203 | if (ret) { |
Ben Dooks | b1c6e6d | 2009-11-02 18:12:33 +0000 | [diff] [blame] | 4204 | if (!(chip->options & NAND_SCAN_SILENT_NODEV)) |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 4205 | pr_warn("No NAND device found\n"); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4206 | chip->select_chip(mtd, -1); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4207 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4208 | } |
| 4209 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4210 | nand_maf_id = chip->id.data[0]; |
| 4211 | nand_dev_id = chip->id.data[1]; |
| 4212 | |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 4213 | chip->select_chip(mtd, -1); |
| 4214 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4215 | /* Check for a chip array */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4216 | for (i = 1; i < maxchips; i++) { |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4217 | /* See comment in nand_get_flash_type for reset */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4218 | nand_reset(chip, i); |
| 4219 | |
| 4220 | chip->select_chip(mtd, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4221 | /* Send the command for reading device ID */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4222 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4223 | /* Read manufacturer and device IDs */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4224 | if (nand_maf_id != chip->read_byte(mtd) || |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 4225 | nand_dev_id != chip->read_byte(mtd)) { |
| 4226 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4227 | break; |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 4228 | } |
| 4229 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4230 | } |
| 4231 | if (i > 1) |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4232 | pr_info("%d chips detected\n", i); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4234 | /* Store the number of chips and calc total size for mtd */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4235 | chip->numchips = i; |
| 4236 | mtd->size = i * chip->chipsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4238 | return 0; |
| 4239 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4240 | EXPORT_SYMBOL(nand_scan_ident); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4241 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4242 | static int nand_set_ecc_soft_ops(struct mtd_info *mtd) |
| 4243 | { |
| 4244 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 4245 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 4246 | |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 4247 | if (WARN_ON(ecc->mode != NAND_ECC_SOFT)) |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4248 | return -EINVAL; |
| 4249 | |
| 4250 | switch (ecc->algo) { |
| 4251 | case NAND_ECC_HAMMING: |
| 4252 | ecc->calculate = nand_calculate_ecc; |
| 4253 | ecc->correct = nand_correct_data; |
| 4254 | ecc->read_page = nand_read_page_swecc; |
| 4255 | ecc->read_subpage = nand_read_subpage; |
| 4256 | ecc->write_page = nand_write_page_swecc; |
| 4257 | ecc->read_page_raw = nand_read_page_raw; |
| 4258 | ecc->write_page_raw = nand_write_page_raw; |
| 4259 | ecc->read_oob = nand_read_oob_std; |
| 4260 | ecc->write_oob = nand_write_oob_std; |
| 4261 | if (!ecc->size) |
| 4262 | ecc->size = 256; |
| 4263 | ecc->bytes = 3; |
| 4264 | ecc->strength = 1; |
| 4265 | return 0; |
| 4266 | case NAND_ECC_BCH: |
| 4267 | if (!mtd_nand_has_bch()) { |
| 4268 | WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n"); |
| 4269 | return -EINVAL; |
| 4270 | } |
| 4271 | ecc->calculate = nand_bch_calculate_ecc; |
| 4272 | ecc->correct = nand_bch_correct_data; |
| 4273 | ecc->read_page = nand_read_page_swecc; |
| 4274 | ecc->read_subpage = nand_read_subpage; |
| 4275 | ecc->write_page = nand_write_page_swecc; |
| 4276 | ecc->read_page_raw = nand_read_page_raw; |
| 4277 | ecc->write_page_raw = nand_write_page_raw; |
| 4278 | ecc->read_oob = nand_read_oob_std; |
| 4279 | ecc->write_oob = nand_write_oob_std; |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 4280 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4281 | /* |
| 4282 | * Board driver should supply ecc.size and ecc.strength |
| 4283 | * values to select how many bits are correctable. |
| 4284 | * Otherwise, default to 4 bits for large page devices. |
| 4285 | */ |
| 4286 | if (!ecc->size && (mtd->oobsize >= 64)) { |
| 4287 | ecc->size = 512; |
| 4288 | ecc->strength = 4; |
| 4289 | } |
| 4290 | |
| 4291 | /* |
| 4292 | * if no ecc placement scheme was provided pickup the default |
| 4293 | * large page one. |
| 4294 | */ |
| 4295 | if (!mtd->ooblayout) { |
| 4296 | /* handle large page devices only */ |
| 4297 | if (mtd->oobsize < 64) { |
| 4298 | WARN(1, "OOB layout is required when using software BCH on small pages\n"); |
| 4299 | return -EINVAL; |
| 4300 | } |
| 4301 | |
| 4302 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops); |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 4303 | |
| 4304 | } |
| 4305 | |
| 4306 | /* |
| 4307 | * We can only maximize ECC config when the default layout is |
| 4308 | * used, otherwise we don't know how many bytes can really be |
| 4309 | * used. |
| 4310 | */ |
| 4311 | if (mtd->ooblayout == &nand_ooblayout_lp_ops && |
| 4312 | ecc->options & NAND_ECC_MAXIMIZE) { |
| 4313 | int steps, bytes; |
| 4314 | |
| 4315 | /* Always prefer 1k blocks over 512bytes ones */ |
| 4316 | ecc->size = 1024; |
| 4317 | steps = mtd->writesize / ecc->size; |
| 4318 | |
| 4319 | /* Reserve 2 bytes for the BBM */ |
| 4320 | bytes = (mtd->oobsize - 2) / steps; |
| 4321 | ecc->strength = bytes * 8 / fls(8 * ecc->size); |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4322 | } |
| 4323 | |
| 4324 | /* See nand_bch_init() for details. */ |
| 4325 | ecc->bytes = 0; |
| 4326 | ecc->priv = nand_bch_init(mtd); |
| 4327 | if (!ecc->priv) { |
| 4328 | WARN(1, "BCH ECC initialization failed!\n"); |
| 4329 | return -EINVAL; |
| 4330 | } |
| 4331 | return 0; |
| 4332 | default: |
| 4333 | WARN(1, "Unsupported ECC algorithm!\n"); |
| 4334 | return -EINVAL; |
| 4335 | } |
| 4336 | } |
| 4337 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 4338 | /** |
| 4339 | * nand_check_ecc_caps - check the sanity of preset ECC settings |
| 4340 | * @chip: nand chip info structure |
| 4341 | * @caps: ECC caps info structure |
| 4342 | * @oobavail: OOB size that the ECC engine can use |
| 4343 | * |
| 4344 | * When ECC step size and strength are already set, check if they are supported |
| 4345 | * by the controller and the calculated ECC bytes fit within the chip's OOB. |
| 4346 | * On success, the calculated ECC bytes is set. |
| 4347 | */ |
| 4348 | int nand_check_ecc_caps(struct nand_chip *chip, |
| 4349 | const struct nand_ecc_caps *caps, int oobavail) |
| 4350 | { |
| 4351 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 4352 | const struct nand_ecc_step_info *stepinfo; |
| 4353 | int preset_step = chip->ecc.size; |
| 4354 | int preset_strength = chip->ecc.strength; |
| 4355 | int nsteps, ecc_bytes; |
| 4356 | int i, j; |
| 4357 | |
| 4358 | if (WARN_ON(oobavail < 0)) |
| 4359 | return -EINVAL; |
| 4360 | |
| 4361 | if (!preset_step || !preset_strength) |
| 4362 | return -ENODATA; |
| 4363 | |
| 4364 | nsteps = mtd->writesize / preset_step; |
| 4365 | |
| 4366 | for (i = 0; i < caps->nstepinfos; i++) { |
| 4367 | stepinfo = &caps->stepinfos[i]; |
| 4368 | |
| 4369 | if (stepinfo->stepsize != preset_step) |
| 4370 | continue; |
| 4371 | |
| 4372 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 4373 | if (stepinfo->strengths[j] != preset_strength) |
| 4374 | continue; |
| 4375 | |
| 4376 | ecc_bytes = caps->calc_ecc_bytes(preset_step, |
| 4377 | preset_strength); |
| 4378 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 4379 | return ecc_bytes; |
| 4380 | |
| 4381 | if (ecc_bytes * nsteps > oobavail) { |
| 4382 | pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB", |
| 4383 | preset_step, preset_strength); |
| 4384 | return -ENOSPC; |
| 4385 | } |
| 4386 | |
| 4387 | chip->ecc.bytes = ecc_bytes; |
| 4388 | |
| 4389 | return 0; |
| 4390 | } |
| 4391 | } |
| 4392 | |
| 4393 | pr_err("ECC (step, strength) = (%d, %d) not supported on this controller", |
| 4394 | preset_step, preset_strength); |
| 4395 | |
| 4396 | return -ENOTSUPP; |
| 4397 | } |
| 4398 | EXPORT_SYMBOL_GPL(nand_check_ecc_caps); |
| 4399 | |
| 4400 | /** |
| 4401 | * nand_match_ecc_req - meet the chip's requirement with least ECC bytes |
| 4402 | * @chip: nand chip info structure |
| 4403 | * @caps: ECC engine caps info structure |
| 4404 | * @oobavail: OOB size that the ECC engine can use |
| 4405 | * |
| 4406 | * If a chip's ECC requirement is provided, try to meet it with the least |
| 4407 | * number of ECC bytes (i.e. with the largest number of OOB-free bytes). |
| 4408 | * On success, the chosen ECC settings are set. |
| 4409 | */ |
| 4410 | int nand_match_ecc_req(struct nand_chip *chip, |
| 4411 | const struct nand_ecc_caps *caps, int oobavail) |
| 4412 | { |
| 4413 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 4414 | const struct nand_ecc_step_info *stepinfo; |
| 4415 | int req_step = chip->ecc_step_ds; |
| 4416 | int req_strength = chip->ecc_strength_ds; |
| 4417 | int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total; |
| 4418 | int best_step, best_strength, best_ecc_bytes; |
| 4419 | int best_ecc_bytes_total = INT_MAX; |
| 4420 | int i, j; |
| 4421 | |
| 4422 | if (WARN_ON(oobavail < 0)) |
| 4423 | return -EINVAL; |
| 4424 | |
| 4425 | /* No information provided by the NAND chip */ |
| 4426 | if (!req_step || !req_strength) |
| 4427 | return -ENOTSUPP; |
| 4428 | |
| 4429 | /* number of correctable bits the chip requires in a page */ |
| 4430 | req_corr = mtd->writesize / req_step * req_strength; |
| 4431 | |
| 4432 | for (i = 0; i < caps->nstepinfos; i++) { |
| 4433 | stepinfo = &caps->stepinfos[i]; |
| 4434 | step_size = stepinfo->stepsize; |
| 4435 | |
| 4436 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 4437 | strength = stepinfo->strengths[j]; |
| 4438 | |
| 4439 | /* |
| 4440 | * If both step size and strength are smaller than the |
| 4441 | * chip's requirement, it is not easy to compare the |
| 4442 | * resulted reliability. |
| 4443 | */ |
| 4444 | if (step_size < req_step && strength < req_strength) |
| 4445 | continue; |
| 4446 | |
| 4447 | if (mtd->writesize % step_size) |
| 4448 | continue; |
| 4449 | |
| 4450 | nsteps = mtd->writesize / step_size; |
| 4451 | |
| 4452 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 4453 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 4454 | continue; |
| 4455 | ecc_bytes_total = ecc_bytes * nsteps; |
| 4456 | |
| 4457 | if (ecc_bytes_total > oobavail || |
| 4458 | strength * nsteps < req_corr) |
| 4459 | continue; |
| 4460 | |
| 4461 | /* |
| 4462 | * We assume the best is to meet the chip's requrement |
| 4463 | * with the least number of ECC bytes. |
| 4464 | */ |
| 4465 | if (ecc_bytes_total < best_ecc_bytes_total) { |
| 4466 | best_ecc_bytes_total = ecc_bytes_total; |
| 4467 | best_step = step_size; |
| 4468 | best_strength = strength; |
| 4469 | best_ecc_bytes = ecc_bytes; |
| 4470 | } |
| 4471 | } |
| 4472 | } |
| 4473 | |
| 4474 | if (best_ecc_bytes_total == INT_MAX) |
| 4475 | return -ENOTSUPP; |
| 4476 | |
| 4477 | chip->ecc.size = best_step; |
| 4478 | chip->ecc.strength = best_strength; |
| 4479 | chip->ecc.bytes = best_ecc_bytes; |
| 4480 | |
| 4481 | return 0; |
| 4482 | } |
| 4483 | EXPORT_SYMBOL_GPL(nand_match_ecc_req); |
| 4484 | |
| 4485 | /** |
| 4486 | * nand_maximize_ecc - choose the max ECC strength available |
| 4487 | * @chip: nand chip info structure |
| 4488 | * @caps: ECC engine caps info structure |
| 4489 | * @oobavail: OOB size that the ECC engine can use |
| 4490 | * |
| 4491 | * Choose the max ECC strength that is supported on the controller, and can fit |
| 4492 | * within the chip's OOB. On success, the chosen ECC settings are set. |
| 4493 | */ |
| 4494 | int nand_maximize_ecc(struct nand_chip *chip, |
| 4495 | const struct nand_ecc_caps *caps, int oobavail) |
| 4496 | { |
| 4497 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 4498 | const struct nand_ecc_step_info *stepinfo; |
| 4499 | int step_size, strength, nsteps, ecc_bytes, corr; |
| 4500 | int best_corr = 0; |
| 4501 | int best_step = 0; |
| 4502 | int best_strength, best_ecc_bytes; |
| 4503 | int i, j; |
| 4504 | |
| 4505 | if (WARN_ON(oobavail < 0)) |
| 4506 | return -EINVAL; |
| 4507 | |
| 4508 | for (i = 0; i < caps->nstepinfos; i++) { |
| 4509 | stepinfo = &caps->stepinfos[i]; |
| 4510 | step_size = stepinfo->stepsize; |
| 4511 | |
| 4512 | /* If chip->ecc.size is already set, respect it */ |
| 4513 | if (chip->ecc.size && step_size != chip->ecc.size) |
| 4514 | continue; |
| 4515 | |
| 4516 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 4517 | strength = stepinfo->strengths[j]; |
| 4518 | |
| 4519 | if (mtd->writesize % step_size) |
| 4520 | continue; |
| 4521 | |
| 4522 | nsteps = mtd->writesize / step_size; |
| 4523 | |
| 4524 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 4525 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 4526 | continue; |
| 4527 | |
| 4528 | if (ecc_bytes * nsteps > oobavail) |
| 4529 | continue; |
| 4530 | |
| 4531 | corr = strength * nsteps; |
| 4532 | |
| 4533 | /* |
| 4534 | * If the number of correctable bits is the same, |
| 4535 | * bigger step_size has more reliability. |
| 4536 | */ |
| 4537 | if (corr > best_corr || |
| 4538 | (corr == best_corr && step_size > best_step)) { |
| 4539 | best_corr = corr; |
| 4540 | best_step = step_size; |
| 4541 | best_strength = strength; |
| 4542 | best_ecc_bytes = ecc_bytes; |
| 4543 | } |
| 4544 | } |
| 4545 | } |
| 4546 | |
| 4547 | if (!best_corr) |
| 4548 | return -ENOTSUPP; |
| 4549 | |
| 4550 | chip->ecc.size = best_step; |
| 4551 | chip->ecc.strength = best_strength; |
| 4552 | chip->ecc.bytes = best_ecc_bytes; |
| 4553 | |
| 4554 | return 0; |
| 4555 | } |
| 4556 | EXPORT_SYMBOL_GPL(nand_maximize_ecc); |
| 4557 | |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 4558 | /* |
| 4559 | * Check if the chip configuration meet the datasheet requirements. |
| 4560 | |
| 4561 | * If our configuration corrects A bits per B bytes and the minimum |
| 4562 | * required correction level is X bits per Y bytes, then we must ensure |
| 4563 | * both of the following are true: |
| 4564 | * |
| 4565 | * (1) A / B >= X / Y |
| 4566 | * (2) A >= X |
| 4567 | * |
| 4568 | * Requirement (1) ensures we can correct for the required bitflip density. |
| 4569 | * Requirement (2) ensures we can correct even when all bitflips are clumped |
| 4570 | * in the same sector. |
| 4571 | */ |
| 4572 | static bool nand_ecc_strength_good(struct mtd_info *mtd) |
| 4573 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4574 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 4575 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 4576 | int corr, ds_corr; |
| 4577 | |
| 4578 | if (ecc->size == 0 || chip->ecc_step_ds == 0) |
| 4579 | /* Not enough information */ |
| 4580 | return true; |
| 4581 | |
| 4582 | /* |
| 4583 | * We get the number of corrected bits per page to compare |
| 4584 | * the correction density. |
| 4585 | */ |
| 4586 | corr = (mtd->writesize * ecc->strength) / ecc->size; |
| 4587 | ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds; |
| 4588 | |
| 4589 | return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds; |
| 4590 | } |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4591 | |
Marc Gonzalez | 3371d66 | 2016-11-15 10:56:20 +0100 | [diff] [blame] | 4592 | static bool invalid_ecc_page_accessors(struct nand_chip *chip) |
| 4593 | { |
| 4594 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 4595 | |
| 4596 | if (nand_standard_page_accessors(ecc)) |
| 4597 | return false; |
| 4598 | |
| 4599 | /* |
| 4600 | * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND |
| 4601 | * controller driver implements all the page accessors because |
| 4602 | * default helpers are not suitable when the core does not |
| 4603 | * send the READ0/PAGEPROG commands. |
| 4604 | */ |
| 4605 | return (!ecc->read_page || !ecc->write_page || |
| 4606 | !ecc->read_page_raw || !ecc->write_page_raw || |
| 4607 | (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) || |
| 4608 | (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage && |
| 4609 | ecc->hwctl && ecc->calculate)); |
| 4610 | } |
| 4611 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4612 | /** |
| 4613 | * nand_scan_tail - [NAND Interface] Scan for the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4614 | * @mtd: MTD device structure |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4615 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4616 | * This is the second phase of the normal nand_scan() function. It fills out |
| 4617 | * all the uninitialized function pointers with the defaults and scans for a |
| 4618 | * bad block table if appropriate. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4619 | */ |
| 4620 | int nand_scan_tail(struct mtd_info *mtd) |
| 4621 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4622 | struct nand_chip *chip = mtd_to_nand(mtd); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4623 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 4624 | struct nand_buffers *nbuf = NULL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4625 | int ret, i; |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4626 | |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 4627 | /* 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] | 4628 | if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) && |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 4629 | !(chip->bbt_options & NAND_BBT_USE_FLASH))) { |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4630 | return -EINVAL; |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 4631 | } |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 4632 | |
Marc Gonzalez | 3371d66 | 2016-11-15 10:56:20 +0100 | [diff] [blame] | 4633 | if (invalid_ecc_page_accessors(chip)) { |
| 4634 | pr_err("Invalid ECC page accessors setup\n"); |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4635 | return -EINVAL; |
Marc Gonzalez | 3371d66 | 2016-11-15 10:56:20 +0100 | [diff] [blame] | 4636 | } |
| 4637 | |
Huang Shijie | f02ea4e | 2014-01-13 14:27:12 +0800 | [diff] [blame] | 4638 | if (!(chip->options & NAND_OWN_BUFFERS)) { |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 4639 | nbuf = kzalloc(sizeof(*nbuf), GFP_KERNEL); |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4640 | if (!nbuf) |
| 4641 | return -ENOMEM; |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 4642 | |
| 4643 | nbuf->ecccalc = kmalloc(mtd->oobsize, GFP_KERNEL); |
| 4644 | if (!nbuf->ecccalc) { |
| 4645 | ret = -ENOMEM; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4646 | goto err_free_nbuf; |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 4647 | } |
| 4648 | |
| 4649 | nbuf->ecccode = kmalloc(mtd->oobsize, GFP_KERNEL); |
| 4650 | if (!nbuf->ecccode) { |
| 4651 | ret = -ENOMEM; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4652 | goto err_free_nbuf; |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 4653 | } |
| 4654 | |
| 4655 | nbuf->databuf = kmalloc(mtd->writesize + mtd->oobsize, |
| 4656 | GFP_KERNEL); |
| 4657 | if (!nbuf->databuf) { |
| 4658 | ret = -ENOMEM; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4659 | goto err_free_nbuf; |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 4660 | } |
Huang Shijie | f02ea4e | 2014-01-13 14:27:12 +0800 | [diff] [blame] | 4661 | |
| 4662 | chip->buffers = nbuf; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4663 | } else if (!chip->buffers) { |
| 4664 | return -ENOMEM; |
Huang Shijie | f02ea4e | 2014-01-13 14:27:12 +0800 | [diff] [blame] | 4665 | } |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 4666 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4667 | /* |
| 4668 | * FIXME: some NAND manufacturer drivers expect the first die to be |
| 4669 | * selected when manufacturer->init() is called. They should be fixed |
| 4670 | * to explictly select the relevant die when interacting with the NAND |
| 4671 | * chip. |
| 4672 | */ |
| 4673 | chip->select_chip(mtd, 0); |
| 4674 | ret = nand_manufacturer_init(chip); |
| 4675 | chip->select_chip(mtd, -1); |
| 4676 | if (ret) |
| 4677 | goto err_free_nbuf; |
| 4678 | |
David Woodhouse | 7dcdcbef | 2006-10-21 17:09:53 +0100 | [diff] [blame] | 4679 | /* Set the internal oob buffer location, just after the page data */ |
David Woodhouse | 784f4d5 | 2006-10-22 01:47:45 +0100 | [diff] [blame] | 4680 | chip->oob_poi = chip->buffers->databuf + mtd->writesize; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4681 | |
| 4682 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4683 | * If no default placement scheme is given, select an appropriate one. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4684 | */ |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4685 | if (!mtd->ooblayout && |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 4686 | !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4687 | switch (mtd->oobsize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4688 | case 8: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | case 16: |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 4690 | mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4691 | break; |
| 4692 | case 64: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 4693 | case 128: |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 4694 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops); |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 4695 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4696 | default: |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4697 | WARN(1, "No oob scheme defined for oobsize %d\n", |
| 4698 | mtd->oobsize); |
| 4699 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4700 | goto err_nand_manuf_cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4701 | } |
| 4702 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4703 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4704 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4705 | * Check ECC mode, default to software if 3byte/512byte hardware ECC is |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4706 | * selected and we have 256 byte pagesize fallback to software ECC |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4707 | */ |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 4708 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4709 | switch (ecc->mode) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 4710 | case NAND_ECC_HW_OOB_FIRST: |
| 4711 | /* Similar to NAND_ECC_HW, but a separate read_page handle */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4712 | if (!ecc->calculate || !ecc->correct || !ecc->hwctl) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4713 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 4714 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4715 | goto err_nand_manuf_cleanup; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 4716 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4717 | if (!ecc->read_page) |
| 4718 | ecc->read_page = nand_read_page_hwecc_oob_first; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 4719 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 4720 | case NAND_ECC_HW: |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4721 | /* Use standard hwecc read page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4722 | if (!ecc->read_page) |
| 4723 | ecc->read_page = nand_read_page_hwecc; |
| 4724 | if (!ecc->write_page) |
| 4725 | ecc->write_page = nand_write_page_hwecc; |
| 4726 | if (!ecc->read_page_raw) |
| 4727 | ecc->read_page_raw = nand_read_page_raw; |
| 4728 | if (!ecc->write_page_raw) |
| 4729 | ecc->write_page_raw = nand_write_page_raw; |
| 4730 | if (!ecc->read_oob) |
| 4731 | ecc->read_oob = nand_read_oob_std; |
| 4732 | if (!ecc->write_oob) |
| 4733 | ecc->write_oob = nand_write_oob_std; |
| 4734 | if (!ecc->read_subpage) |
| 4735 | ecc->read_subpage = nand_read_subpage; |
Helmut Schaa | 44991b3 | 2014-04-09 11:13:24 +0200 | [diff] [blame] | 4736 | if (!ecc->write_subpage && ecc->hwctl && ecc->calculate) |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4737 | ecc->write_subpage = nand_write_subpage_hwecc; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 4738 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 4739 | case NAND_ECC_HW_SYNDROME: |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4740 | if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) && |
| 4741 | (!ecc->read_page || |
| 4742 | ecc->read_page == nand_read_page_hwecc || |
| 4743 | !ecc->write_page || |
| 4744 | ecc->write_page == nand_write_page_hwecc)) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4745 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 4746 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4747 | goto err_nand_manuf_cleanup; |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 4748 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4749 | /* Use standard syndrome read/write page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4750 | if (!ecc->read_page) |
| 4751 | ecc->read_page = nand_read_page_syndrome; |
| 4752 | if (!ecc->write_page) |
| 4753 | ecc->write_page = nand_write_page_syndrome; |
| 4754 | if (!ecc->read_page_raw) |
| 4755 | ecc->read_page_raw = nand_read_page_raw_syndrome; |
| 4756 | if (!ecc->write_page_raw) |
| 4757 | ecc->write_page_raw = nand_write_page_raw_syndrome; |
| 4758 | if (!ecc->read_oob) |
| 4759 | ecc->read_oob = nand_read_oob_syndrome; |
| 4760 | if (!ecc->write_oob) |
| 4761 | ecc->write_oob = nand_write_oob_syndrome; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 4762 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4763 | if (mtd->writesize >= ecc->size) { |
| 4764 | if (!ecc->strength) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4765 | WARN(1, "Driver must set ecc.strength when using hardware ECC\n"); |
| 4766 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4767 | goto err_nand_manuf_cleanup; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 4768 | } |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 4769 | break; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 4770 | } |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 4771 | pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n", |
| 4772 | ecc->size, mtd->writesize); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4773 | ecc->mode = NAND_ECC_SOFT; |
Rafał Miłecki | e9d4fae | 2016-04-17 22:53:02 +0200 | [diff] [blame] | 4774 | ecc->algo = NAND_ECC_HAMMING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4775 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 4776 | case NAND_ECC_SOFT: |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4777 | ret = nand_set_ecc_soft_ops(mtd); |
| 4778 | if (ret) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4779 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4780 | goto err_nand_manuf_cleanup; |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 4781 | } |
| 4782 | break; |
| 4783 | |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 4784 | case NAND_ECC_ON_DIE: |
| 4785 | if (!ecc->read_page || !ecc->write_page) { |
| 4786 | WARN(1, "No ECC functions supplied; on-die ECC not possible\n"); |
| 4787 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4788 | goto err_nand_manuf_cleanup; |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 4789 | } |
| 4790 | if (!ecc->read_oob) |
| 4791 | ecc->read_oob = nand_read_oob_std; |
| 4792 | if (!ecc->write_oob) |
| 4793 | ecc->write_oob = nand_write_oob_std; |
| 4794 | break; |
| 4795 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4796 | case NAND_ECC_NONE: |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 4797 | 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] | 4798 | ecc->read_page = nand_read_page_raw; |
| 4799 | ecc->write_page = nand_write_page_raw; |
| 4800 | ecc->read_oob = nand_read_oob_std; |
| 4801 | ecc->read_page_raw = nand_read_page_raw; |
| 4802 | ecc->write_page_raw = nand_write_page_raw; |
| 4803 | ecc->write_oob = nand_write_oob_std; |
| 4804 | ecc->size = mtd->writesize; |
| 4805 | ecc->bytes = 0; |
| 4806 | ecc->strength = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4807 | break; |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 4808 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4809 | default: |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4810 | WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode); |
| 4811 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4812 | goto err_nand_manuf_cleanup; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4813 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4814 | |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 4815 | /* For many systems, the standard OOB write also works for raw */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4816 | if (!ecc->read_oob_raw) |
| 4817 | ecc->read_oob_raw = ecc->read_oob; |
| 4818 | if (!ecc->write_oob_raw) |
| 4819 | ecc->write_oob_raw = ecc->write_oob; |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 4820 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4821 | /* propagate ecc info to mtd_info */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4822 | mtd->ecc_strength = ecc->strength; |
| 4823 | mtd->ecc_step_size = ecc->size; |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 4824 | |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 4825 | /* |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4826 | * 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] | 4827 | * mode. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4828 | */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4829 | ecc->steps = mtd->writesize / ecc->size; |
| 4830 | if (ecc->steps * ecc->size != mtd->writesize) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4831 | WARN(1, "Invalid ECC parameters\n"); |
| 4832 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4833 | goto err_nand_manuf_cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4834 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4835 | ecc->total = ecc->steps * ecc->bytes; |
Masahiro Yamada | 79e0348 | 2017-05-25 13:50:20 +0900 | [diff] [blame] | 4836 | if (ecc->total > mtd->oobsize) { |
| 4837 | WARN(1, "Total number of ECC bytes exceeded oobsize\n"); |
| 4838 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4839 | goto err_nand_manuf_cleanup; |
Masahiro Yamada | 79e0348 | 2017-05-25 13:50:20 +0900 | [diff] [blame] | 4840 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4841 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4842 | /* |
| 4843 | * The number of bytes available for a client to place data into |
| 4844 | * the out of band area. |
| 4845 | */ |
| 4846 | ret = mtd_ooblayout_count_freebytes(mtd); |
| 4847 | if (ret < 0) |
| 4848 | ret = 0; |
| 4849 | |
| 4850 | mtd->oobavail = ret; |
| 4851 | |
| 4852 | /* ECC sanity check: warn if it's too weak */ |
| 4853 | if (!nand_ecc_strength_good(mtd)) |
| 4854 | pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n", |
| 4855 | mtd->name); |
| 4856 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4857 | /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */ |
Huang Shijie | 1d0ed69 | 2013-09-25 14:58:10 +0800 | [diff] [blame] | 4858 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) { |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4859 | switch (ecc->steps) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4860 | case 2: |
| 4861 | mtd->subpage_sft = 1; |
| 4862 | break; |
| 4863 | case 4: |
| 4864 | case 8: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 4865 | case 16: |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4866 | mtd->subpage_sft = 2; |
| 4867 | break; |
| 4868 | } |
| 4869 | } |
| 4870 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; |
| 4871 | |
Thomas Gleixner | 04bbd0e | 2006-05-25 09:45:29 +0200 | [diff] [blame] | 4872 | /* Initialize state */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4873 | chip->state = FL_READY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4874 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4875 | /* Invalidate the pagebuffer reference */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4876 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4877 | |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 4878 | /* Large page NAND with SOFT_ECC should support subpage reads */ |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 4879 | switch (ecc->mode) { |
| 4880 | case NAND_ECC_SOFT: |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 4881 | if (chip->page_shift > 9) |
| 4882 | chip->options |= NAND_SUBPAGE_READ; |
| 4883 | break; |
| 4884 | |
| 4885 | default: |
| 4886 | break; |
| 4887 | } |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 4888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4889 | /* Fill in remaining MTD driver data */ |
Huang Shijie | 963d1c2 | 2013-09-25 14:58:21 +0800 | [diff] [blame] | 4890 | mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH; |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 4891 | mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM : |
| 4892 | MTD_CAP_NANDFLASH; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 4893 | mtd->_erase = nand_erase; |
| 4894 | mtd->_point = NULL; |
| 4895 | mtd->_unpoint = NULL; |
| 4896 | mtd->_read = nand_read; |
| 4897 | mtd->_write = nand_write; |
| 4898 | mtd->_panic_write = panic_nand_write; |
| 4899 | mtd->_read_oob = nand_read_oob; |
| 4900 | mtd->_write_oob = nand_write_oob; |
| 4901 | mtd->_sync = nand_sync; |
| 4902 | mtd->_lock = NULL; |
| 4903 | mtd->_unlock = NULL; |
| 4904 | mtd->_suspend = nand_suspend; |
| 4905 | mtd->_resume = nand_resume; |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 4906 | mtd->_reboot = nand_shutdown; |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 4907 | mtd->_block_isreserved = nand_block_isreserved; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 4908 | mtd->_block_isbad = nand_block_isbad; |
| 4909 | mtd->_block_markbad = nand_block_markbad; |
Zach Brown | 5671842 | 2017-01-10 13:30:20 -0600 | [diff] [blame] | 4910 | mtd->_max_bad_blocks = nand_max_bad_blocks; |
Anatolij Gustschin | cbcab65 | 2010-12-16 23:42:16 +0100 | [diff] [blame] | 4911 | mtd->writebufsize = mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4912 | |
Shmulik Ladkani | ea3b2ea | 2012-06-08 18:29:06 +0300 | [diff] [blame] | 4913 | /* |
| 4914 | * Initialize bitflip_threshold to its default prior scan_bbt() call. |
| 4915 | * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be |
| 4916 | * properly set. |
| 4917 | */ |
| 4918 | if (!mtd->bitflip_threshold) |
Brian Norris | 240181f | 2015-01-12 12:51:29 -0800 | [diff] [blame] | 4919 | mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4920 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4921 | /* Initialize the ->data_interface field. */ |
| 4922 | ret = nand_init_data_interface(chip); |
| 4923 | if (ret) |
| 4924 | goto err_nand_manuf_cleanup; |
| 4925 | |
| 4926 | /* Enter fastest possible mode on all dies. */ |
| 4927 | for (i = 0; i < chip->numchips; i++) { |
| 4928 | chip->select_chip(mtd, i); |
| 4929 | ret = nand_setup_data_interface(chip, i); |
| 4930 | chip->select_chip(mtd, -1); |
| 4931 | |
| 4932 | if (ret) |
| 4933 | goto err_nand_data_iface_cleanup; |
| 4934 | } |
| 4935 | |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 4936 | /* Check, if we should skip the bad block table scan */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4937 | if (chip->options & NAND_SKIP_BBTSCAN) |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 4938 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4939 | |
| 4940 | /* Build bad block table */ |
Brian Norris | 44d4182 | 2017-05-01 17:04:50 -0700 | [diff] [blame] | 4941 | ret = chip->scan_bbt(mtd); |
| 4942 | if (ret) |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4943 | goto err_nand_data_iface_cleanup; |
| 4944 | |
Brian Norris | 44d4182 | 2017-05-01 17:04:50 -0700 | [diff] [blame] | 4945 | return 0; |
| 4946 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 4947 | err_nand_data_iface_cleanup: |
| 4948 | nand_release_data_interface(chip); |
| 4949 | |
| 4950 | err_nand_manuf_cleanup: |
| 4951 | nand_manufacturer_cleanup(chip); |
| 4952 | |
| 4953 | err_free_nbuf: |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 4954 | if (nbuf) { |
| 4955 | kfree(nbuf->databuf); |
| 4956 | kfree(nbuf->ecccode); |
| 4957 | kfree(nbuf->ecccalc); |
| 4958 | kfree(nbuf); |
| 4959 | } |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 4960 | |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4961 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4962 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4963 | EXPORT_SYMBOL(nand_scan_tail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4964 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4965 | /* |
| 4966 | * 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] | 4967 | * 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] | 4968 | * to call us from in-kernel code if the core NAND support is modular. |
| 4969 | */ |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4970 | #ifdef MODULE |
| 4971 | #define caller_is_module() (1) |
| 4972 | #else |
| 4973 | #define caller_is_module() \ |
Rusty Russell | a6e6abd | 2009-03-31 13:05:31 -0600 | [diff] [blame] | 4974 | is_module_text_address((unsigned long)__builtin_return_address(0)) |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4975 | #endif |
| 4976 | |
| 4977 | /** |
| 4978 | * nand_scan - [NAND Interface] Scan for the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4979 | * @mtd: MTD device structure |
| 4980 | * @maxchips: number of chips to scan for |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4981 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4982 | * This fills out all the uninitialized function pointers with the defaults. |
| 4983 | * 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] | 4984 | * appropriate values. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4985 | */ |
| 4986 | int nand_scan(struct mtd_info *mtd, int maxchips) |
| 4987 | { |
| 4988 | int ret; |
| 4989 | |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4990 | ret = nand_scan_ident(mtd, maxchips, NULL); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4991 | if (!ret) |
| 4992 | ret = nand_scan_tail(mtd); |
| 4993 | return ret; |
| 4994 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4995 | EXPORT_SYMBOL(nand_scan); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4996 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4997 | /** |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 4998 | * nand_cleanup - [NAND Interface] Free resources held by the NAND device |
| 4999 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5000 | */ |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5001 | void nand_cleanup(struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5002 | { |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 5003 | if (chip->ecc.mode == NAND_ECC_SOFT && |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 5004 | chip->ecc.algo == NAND_ECC_BCH) |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 5005 | nand_bch_free((struct nand_bch_control *)chip->ecc.priv); |
| 5006 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 5007 | nand_release_data_interface(chip); |
| 5008 | |
Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 5009 | /* Free bad block table memory */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5010 | kfree(chip->bbt); |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 5011 | if (!(chip->options & NAND_OWN_BUFFERS) && chip->buffers) { |
| 5012 | kfree(chip->buffers->databuf); |
| 5013 | kfree(chip->buffers->ecccode); |
| 5014 | kfree(chip->buffers->ecccalc); |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 5015 | kfree(chip->buffers); |
Masahiro Yamada | 3deb997 | 2017-03-30 17:15:04 +0900 | [diff] [blame] | 5016 | } |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 5017 | |
| 5018 | /* Free bad block descriptor memory */ |
| 5019 | if (chip->badblock_pattern && chip->badblock_pattern->options |
| 5020 | & NAND_BBT_DYNAMICSTRUCT) |
| 5021 | kfree(chip->badblock_pattern); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5022 | |
| 5023 | /* Free manufacturer priv data. */ |
| 5024 | nand_manufacturer_cleanup(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | } |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 5026 | EXPORT_SYMBOL_GPL(nand_cleanup); |
| 5027 | |
| 5028 | /** |
| 5029 | * nand_release - [NAND Interface] Unregister the MTD device and free resources |
| 5030 | * held by the NAND device |
| 5031 | * @mtd: MTD device structure |
| 5032 | */ |
| 5033 | void nand_release(struct mtd_info *mtd) |
| 5034 | { |
| 5035 | mtd_device_unregister(mtd); |
| 5036 | nand_cleanup(mtd_to_nand(mtd)); |
| 5037 | } |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5038 | EXPORT_SYMBOL_GPL(nand_release); |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 5039 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5040 | MODULE_LICENSE("GPL"); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 5041 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>"); |
| 5042 | MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>"); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5043 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |