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