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