Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Overview: |
| 3 | * This is the generic MTD driver for NAND flash devices. It should be |
| 4 | * capable of working with almost all NAND chips currently available. |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Additional technical information is available on |
maximilian attems | 8b2b403 | 2007-07-28 13:07:16 +0200 | [diff] [blame] | 7 | * http://www.linux-mtd.infradead.org/doc/nand.html |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 10 | * 2002-2006 Thomas Gleixner (tglx@linutronix.de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 12 | * Credits: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 13 | * David Woodhouse for adding multichip support |
| 14 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * Aleph One Ltd. and Toby Churchill Ltd. for supporting the |
| 16 | * rework for 2K page size chips |
| 17 | * |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 18 | * TODO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * Enable cached programming for 2k page size chips |
| 20 | * Check, if mtd->ecctype should be set to MTD_ECC_HW |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 21 | * if we have HW ECC support. |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 22 | * BBT table is not serialized, has to be fixed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * This program is free software; you can redistribute it and/or modify |
| 25 | * it under the terms of the GNU General Public License version 2 as |
| 26 | * published by the Free Software Foundation. |
| 27 | * |
| 28 | */ |
| 29 | |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 30 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 31 | |
David Woodhouse | 552d920 | 2006-05-14 01:20:46 +0100 | [diff] [blame] | 32 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/delay.h> |
| 34 | #include <linux/errno.h> |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 35 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/sched.h> |
| 37 | #include <linux/slab.h> |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 38 | #include <linux/mm.h> |
Ingo Molnar | 38b8d20 | 2017-02-08 18:51:31 +0100 | [diff] [blame] | 39 | #include <linux/nmi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/types.h> |
| 41 | #include <linux/mtd/mtd.h> |
Boris Brezillon | d4092d7 | 2017-08-04 17:29:10 +0200 | [diff] [blame] | 42 | #include <linux/mtd/rawnand.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/mtd/nand_ecc.h> |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 44 | #include <linux/mtd/nand_bch.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/interrupt.h> |
| 46 | #include <linux/bitops.h> |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 47 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/mtd/partitions.h> |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 49 | #include <linux/of.h> |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 50 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 51 | static int nand_get_device(struct mtd_info *mtd, int new_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 53 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 54 | struct mtd_oob_ops *ops); |
| 55 | |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 56 | /* Define default oob placement schemes for large and small page devices */ |
| 57 | static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section, |
| 58 | struct mtd_oob_region *oobregion) |
| 59 | { |
| 60 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 61 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 62 | |
| 63 | if (section > 1) |
| 64 | return -ERANGE; |
| 65 | |
| 66 | if (!section) { |
| 67 | oobregion->offset = 0; |
Miquel Raynal | f7f8c17 | 2017-07-05 08:51:09 +0200 | [diff] [blame] | 68 | if (mtd->oobsize == 16) |
| 69 | oobregion->length = 4; |
| 70 | else |
| 71 | oobregion->length = 3; |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 72 | } else { |
Miquel Raynal | f7f8c17 | 2017-07-05 08:51:09 +0200 | [diff] [blame] | 73 | if (mtd->oobsize == 8) |
| 74 | return -ERANGE; |
| 75 | |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 76 | oobregion->offset = 6; |
| 77 | oobregion->length = ecc->total - 4; |
| 78 | } |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section, |
| 84 | struct mtd_oob_region *oobregion) |
| 85 | { |
| 86 | if (section > 1) |
| 87 | return -ERANGE; |
| 88 | |
| 89 | if (mtd->oobsize == 16) { |
| 90 | if (section) |
| 91 | return -ERANGE; |
| 92 | |
| 93 | oobregion->length = 8; |
| 94 | oobregion->offset = 8; |
| 95 | } else { |
| 96 | oobregion->length = 2; |
| 97 | if (!section) |
| 98 | oobregion->offset = 3; |
| 99 | else |
| 100 | oobregion->offset = 6; |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = { |
| 107 | .ecc = nand_ooblayout_ecc_sp, |
| 108 | .free = nand_ooblayout_free_sp, |
| 109 | }; |
| 110 | EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops); |
| 111 | |
| 112 | static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section, |
| 113 | struct mtd_oob_region *oobregion) |
| 114 | { |
| 115 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 116 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 117 | |
Miquel Raynal | 882fd15 | 2017-08-26 17:19:15 +0200 | [diff] [blame] | 118 | if (section || !ecc->total) |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 119 | return -ERANGE; |
| 120 | |
| 121 | oobregion->length = ecc->total; |
| 122 | oobregion->offset = mtd->oobsize - oobregion->length; |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section, |
| 128 | struct mtd_oob_region *oobregion) |
| 129 | { |
| 130 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 131 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 132 | |
| 133 | if (section) |
| 134 | return -ERANGE; |
| 135 | |
| 136 | oobregion->length = mtd->oobsize - ecc->total - 2; |
| 137 | oobregion->offset = 2; |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = { |
| 143 | .ecc = nand_ooblayout_ecc_lp, |
| 144 | .free = nand_ooblayout_free_lp, |
| 145 | }; |
| 146 | EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops); |
Thomas Gleixner | d470a97 | 2006-05-23 23:48:57 +0200 | [diff] [blame] | 147 | |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 148 | /* |
| 149 | * Support the old "large page" layout used for 1-bit Hamming ECC where ECC |
| 150 | * are placed at a fixed offset. |
| 151 | */ |
| 152 | static int nand_ooblayout_ecc_lp_hamming(struct mtd_info *mtd, int section, |
| 153 | struct mtd_oob_region *oobregion) |
| 154 | { |
| 155 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 156 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 157 | |
| 158 | if (section) |
| 159 | return -ERANGE; |
| 160 | |
| 161 | switch (mtd->oobsize) { |
| 162 | case 64: |
| 163 | oobregion->offset = 40; |
| 164 | break; |
| 165 | case 128: |
| 166 | oobregion->offset = 80; |
| 167 | break; |
| 168 | default: |
| 169 | return -EINVAL; |
| 170 | } |
| 171 | |
| 172 | oobregion->length = ecc->total; |
| 173 | if (oobregion->offset + oobregion->length > mtd->oobsize) |
| 174 | return -ERANGE; |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static int nand_ooblayout_free_lp_hamming(struct mtd_info *mtd, int section, |
| 180 | struct mtd_oob_region *oobregion) |
| 181 | { |
| 182 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 183 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 184 | int ecc_offset = 0; |
| 185 | |
| 186 | if (section < 0 || section > 1) |
| 187 | return -ERANGE; |
| 188 | |
| 189 | switch (mtd->oobsize) { |
| 190 | case 64: |
| 191 | ecc_offset = 40; |
| 192 | break; |
| 193 | case 128: |
| 194 | ecc_offset = 80; |
| 195 | break; |
| 196 | default: |
| 197 | return -EINVAL; |
| 198 | } |
| 199 | |
| 200 | if (section == 0) { |
| 201 | oobregion->offset = 2; |
| 202 | oobregion->length = ecc_offset - 2; |
| 203 | } else { |
| 204 | oobregion->offset = ecc_offset + ecc->total; |
| 205 | oobregion->length = mtd->oobsize - oobregion->offset; |
| 206 | } |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
Colin Ian King | d4ed3b9 | 2017-05-04 13:11:00 +0100 | [diff] [blame] | 211 | static const struct mtd_ooblayout_ops nand_ooblayout_lp_hamming_ops = { |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 212 | .ecc = nand_ooblayout_ecc_lp_hamming, |
| 213 | .free = nand_ooblayout_free_lp_hamming, |
| 214 | }; |
| 215 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 216 | static int check_offs_len(struct mtd_info *mtd, |
| 217 | loff_t ofs, uint64_t len) |
| 218 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 219 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 220 | int ret = 0; |
| 221 | |
| 222 | /* Start address must align on block boundary */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 223 | if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 224 | pr_debug("%s: unaligned address\n", __func__); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 225 | ret = -EINVAL; |
| 226 | } |
| 227 | |
| 228 | /* Length must align on block boundary */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 229 | if (len & ((1ULL << chip->phys_erase_shift) - 1)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 230 | pr_debug("%s: length not block aligned\n", __func__); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 231 | ret = -EINVAL; |
| 232 | } |
| 233 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 234 | return ret; |
| 235 | } |
| 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | /** |
| 238 | * nand_release_device - [GENERIC] release chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 239 | * @mtd: MTD device structure |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 240 | * |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 241 | * Release chip lock and wake up anyone waiting on the device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 243 | static void nand_release_device(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 245 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 247 | /* Release the controller and the chip */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 248 | spin_lock(&chip->controller->lock); |
| 249 | chip->controller->active = NULL; |
| 250 | chip->state = FL_READY; |
| 251 | wake_up(&chip->controller->wq); |
| 252 | spin_unlock(&chip->controller->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /** |
| 256 | * nand_read_byte - [DEFAULT] read one byte from the chip |
Boris Brezillon | 7e53432 | 2018-09-06 14:05:22 +0200 | [diff] [blame] | 257 | * @chip: NAND chip object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 259 | * Default read function for 8bit buswidth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | */ |
Boris Brezillon | 7e53432 | 2018-09-06 14:05:22 +0200 | [diff] [blame] | 261 | static uint8_t nand_read_byte(struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | { |
Boris Brezillon | 82fc509 | 2018-09-07 00:38:34 +0200 | [diff] [blame] | 263 | return readb(chip->legacy.IO_ADDR_R); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /** |
Masanari Iida | 064a769 | 2012-11-09 23:20:58 +0900 | [diff] [blame] | 267 | * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip |
Boris Brezillon | 7e53432 | 2018-09-06 14:05:22 +0200 | [diff] [blame] | 268 | * @chip: NAND chip object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 270 | * Default read function for 16bit buswidth with endianness conversion. |
| 271 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | */ |
Boris Brezillon | 7e53432 | 2018-09-06 14:05:22 +0200 | [diff] [blame] | 273 | static uint8_t nand_read_byte16(struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
Boris Brezillon | 82fc509 | 2018-09-07 00:38:34 +0200 | [diff] [blame] | 275 | return (uint8_t) cpu_to_le16(readw(chip->legacy.IO_ADDR_R)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | * nand_select_chip - [DEFAULT] control CE line |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 280 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 281 | * @chipnr: chipnumber to select, -1 for deselect |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | * |
| 283 | * Default select function for 1 chip devices. |
| 284 | */ |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 285 | static void nand_select_chip(struct nand_chip *chip, int chipnr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 287 | switch (chipnr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | case -1: |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 289 | chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE, |
| 290 | 0 | NAND_CTRL_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | break; |
| 292 | case 0: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | break; |
| 294 | |
| 295 | default: |
| 296 | BUG(); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | /** |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 301 | * nand_write_byte - [DEFAULT] write single byte to chip |
Boris Brezillon | c0739d8 | 2018-09-06 14:05:23 +0200 | [diff] [blame] | 302 | * @chip: NAND chip object |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 303 | * @byte: value to write |
| 304 | * |
| 305 | * Default function to write a byte to I/O[7:0] |
| 306 | */ |
Boris Brezillon | c0739d8 | 2018-09-06 14:05:23 +0200 | [diff] [blame] | 307 | static void nand_write_byte(struct nand_chip *chip, uint8_t byte) |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 308 | { |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 309 | chip->legacy.write_buf(chip, &byte, 1); |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /** |
| 313 | * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16 |
Boris Brezillon | c0739d8 | 2018-09-06 14:05:23 +0200 | [diff] [blame] | 314 | * @chip: NAND chip object |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 315 | * @byte: value to write |
| 316 | * |
| 317 | * Default function to write a byte to I/O[7:0] on a 16-bit wide chip. |
| 318 | */ |
Boris Brezillon | c0739d8 | 2018-09-06 14:05:23 +0200 | [diff] [blame] | 319 | static void nand_write_byte16(struct nand_chip *chip, uint8_t byte) |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 320 | { |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 321 | uint16_t word = byte; |
| 322 | |
| 323 | /* |
| 324 | * It's not entirely clear what should happen to I/O[15:8] when writing |
| 325 | * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads: |
| 326 | * |
| 327 | * When the host supports a 16-bit bus width, only data is |
| 328 | * transferred at the 16-bit width. All address and command line |
| 329 | * transfers shall use only the lower 8-bits of the data bus. During |
| 330 | * command transfers, the host may place any value on the upper |
| 331 | * 8-bits of the data bus. During address transfers, the host shall |
| 332 | * set the upper 8-bits of the data bus to 00h. |
| 333 | * |
Miquel Raynal | b958758 | 2018-03-19 14:47:19 +0100 | [diff] [blame] | 334 | * One user of the write_byte callback is nand_set_features. The |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 335 | * four parameters are specified to be written to I/O[7:0], but this is |
| 336 | * neither an address nor a command transfer. Let's assume a 0 on the |
| 337 | * upper I/O lines is OK. |
| 338 | */ |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 339 | chip->legacy.write_buf(chip, (uint8_t *)&word, 2); |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | * nand_write_buf - [DEFAULT] write buffer to chip |
Boris Brezillon | c0739d8 | 2018-09-06 14:05:23 +0200 | [diff] [blame] | 344 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 345 | * @buf: data buffer |
| 346 | * @len: number of bytes to write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 348 | * Default write function for 8bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | */ |
Boris Brezillon | c0739d8 | 2018-09-06 14:05:23 +0200 | [diff] [blame] | 350 | static void nand_write_buf(struct nand_chip *chip, const uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | { |
Boris Brezillon | 82fc509 | 2018-09-07 00:38:34 +0200 | [diff] [blame] | 352 | iowrite8_rep(chip->legacy.IO_ADDR_W, buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 356 | * nand_read_buf - [DEFAULT] read chip data into buffer |
Boris Brezillon | 7e53432 | 2018-09-06 14:05:22 +0200 | [diff] [blame] | 357 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 358 | * @buf: buffer to store date |
| 359 | * @len: number of bytes to read |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 361 | * Default read function for 8bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | */ |
Boris Brezillon | 7e53432 | 2018-09-06 14:05:22 +0200 | [diff] [blame] | 363 | static void nand_read_buf(struct nand_chip *chip, uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | { |
Boris Brezillon | 82fc509 | 2018-09-07 00:38:34 +0200 | [diff] [blame] | 365 | ioread8_rep(chip->legacy.IO_ADDR_R, buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | * nand_write_buf16 - [DEFAULT] write buffer to chip |
Boris Brezillon | c0739d8 | 2018-09-06 14:05:23 +0200 | [diff] [blame] | 370 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 371 | * @buf: data buffer |
| 372 | * @len: number of bytes to write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 374 | * Default write function for 16bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | */ |
Boris Brezillon | c0739d8 | 2018-09-06 14:05:23 +0200 | [diff] [blame] | 376 | static void nand_write_buf16(struct nand_chip *chip, const uint8_t *buf, |
| 377 | int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | u16 *p = (u16 *) buf; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 380 | |
Boris Brezillon | 82fc509 | 2018-09-07 00:38:34 +0200 | [diff] [blame] | 381 | iowrite16_rep(chip->legacy.IO_ADDR_W, p, len >> 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 385 | * nand_read_buf16 - [DEFAULT] read chip data into buffer |
Boris Brezillon | 7e53432 | 2018-09-06 14:05:22 +0200 | [diff] [blame] | 386 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 387 | * @buf: buffer to store date |
| 388 | * @len: number of bytes to read |
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 read function for 16bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | */ |
Boris Brezillon | 7e53432 | 2018-09-06 14:05:22 +0200 | [diff] [blame] | 392 | static void nand_read_buf16(struct nand_chip *chip, uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | u16 *p = (u16 *) buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
Boris Brezillon | 82fc509 | 2018-09-07 00:38:34 +0200 | [diff] [blame] | 396 | ioread16_rep(chip->legacy.IO_ADDR_R, p, len >> 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 401 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 402 | * @ofs: offset from device start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 404 | * Check, if the block is bad. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | */ |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 406 | static int nand_block_bad(struct nand_chip *chip, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 408 | struct mtd_info *mtd = nand_to_mtd(chip); |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 409 | int page, page_end, res; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 410 | u8 bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
Brian Norris | 5fb1549 | 2011-05-31 16:31:21 -0700 | [diff] [blame] | 412 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
Kevin Cernekee | b60b08b | 2010-05-04 20:58:10 -0700 | [diff] [blame] | 413 | ofs += mtd->erasesize - mtd->writesize; |
| 414 | |
Thomas Knobloch | 1a12f46 | 2007-05-03 07:39:37 +0100 | [diff] [blame] | 415 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 416 | page_end = page + (chip->bbt_options & NAND_BBT_SCAN2NDPAGE ? 2 : 1); |
Thomas Knobloch | 1a12f46 | 2007-05-03 07:39:37 +0100 | [diff] [blame] | 417 | |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 418 | for (; page < page_end; page++) { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 419 | res = chip->ecc.read_oob(chip, page); |
Abhishek Sahu | e9893e6 | 2018-06-13 14:32:36 +0530 | [diff] [blame] | 420 | if (res < 0) |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 421 | return res; |
| 422 | |
| 423 | bad = chip->oob_poi[chip->badblockpos]; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 424 | |
Brian Norris | cdbec05 | 2012-01-13 18:11:48 -0800 | [diff] [blame] | 425 | if (likely(chip->badblockbits == 8)) |
| 426 | res = bad != 0xFF; |
| 427 | else |
| 428 | res = hweight8(bad) < chip->badblockbits; |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 429 | if (res) |
| 430 | return res; |
| 431 | } |
Maxim Levitsky | e0b58d0 | 2010-02-22 20:39:38 +0200 | [diff] [blame] | 432 | |
Masahiro Yamada | c120e75 | 2017-03-23 05:07:01 +0900 | [diff] [blame] | 433 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /** |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 437 | * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 438 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 439 | * @ofs: offset from device start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 441 | * This is the default implementation, which can be overridden by a hardware |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 442 | * specific driver. It provides the details for writing a bad block marker to a |
| 443 | * block. |
| 444 | */ |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 445 | static int nand_default_block_markbad(struct nand_chip *chip, loff_t ofs) |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 446 | { |
Boris Brezillon | c17556f | 2018-09-06 14:05:25 +0200 | [diff] [blame] | 447 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 448 | struct mtd_oob_ops ops; |
| 449 | uint8_t buf[2] = { 0, 0 }; |
| 450 | int ret = 0, res, i = 0; |
| 451 | |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 452 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 453 | ops.oobbuf = buf; |
| 454 | ops.ooboffs = chip->badblockpos; |
| 455 | if (chip->options & NAND_BUSWIDTH_16) { |
| 456 | ops.ooboffs &= ~0x01; |
| 457 | ops.len = ops.ooblen = 2; |
| 458 | } else { |
| 459 | ops.len = ops.ooblen = 1; |
| 460 | } |
| 461 | ops.mode = MTD_OPS_PLACE_OOB; |
| 462 | |
| 463 | /* Write to first/last page(s) if necessary */ |
| 464 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
| 465 | ofs += mtd->erasesize - mtd->writesize; |
| 466 | do { |
| 467 | res = nand_do_write_oob(mtd, ofs, &ops); |
| 468 | if (!ret) |
| 469 | ret = res; |
| 470 | |
| 471 | i++; |
| 472 | ofs += mtd->writesize; |
| 473 | } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2); |
| 474 | |
| 475 | return ret; |
| 476 | } |
| 477 | |
| 478 | /** |
Boris Brezillon | cdc784c | 2018-09-07 00:38:38 +0200 | [diff] [blame] | 479 | * nand_markbad_bbm - mark a block by updating the BBM |
| 480 | * @chip: NAND chip object |
| 481 | * @ofs: offset of the block to mark bad |
| 482 | */ |
| 483 | int nand_markbad_bbm(struct nand_chip *chip, loff_t ofs) |
| 484 | { |
| 485 | if (chip->legacy.block_markbad) |
| 486 | return chip->legacy.block_markbad(chip, ofs); |
| 487 | |
| 488 | return nand_default_block_markbad(chip, ofs); |
| 489 | } |
| 490 | |
| 491 | static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs) |
| 492 | { |
| 493 | if (chip->legacy.block_bad) |
| 494 | return chip->legacy.block_bad(chip, ofs); |
| 495 | |
| 496 | return nand_block_bad(chip, ofs); |
| 497 | } |
| 498 | |
| 499 | /** |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 500 | * nand_block_markbad_lowlevel - mark a block bad |
| 501 | * @mtd: MTD device structure |
| 502 | * @ofs: offset from device start |
| 503 | * |
| 504 | * This function performs the generic NAND bad block marking steps (i.e., bad |
| 505 | * block table(s) and/or marker(s)). We only allow the hardware driver to |
Boris Brezillon | cdc784c | 2018-09-07 00:38:38 +0200 | [diff] [blame] | 506 | * specify how to write bad block markers to OOB (chip->legacy.block_markbad). |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 507 | * |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 508 | * We try operations in the following order: |
Mauro Carvalho Chehab | b6f6c29 | 2017-05-13 07:40:36 -0300 | [diff] [blame] | 509 | * |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 510 | * (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] | 511 | * (2) write bad block marker to OOB area of affected block (unless flag |
| 512 | * NAND_BBT_NO_OOB_BBM is present) |
| 513 | * (3) update the BBT |
Mauro Carvalho Chehab | b6f6c29 | 2017-05-13 07:40:36 -0300 | [diff] [blame] | 514 | * |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 515 | * 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] | 516 | * procedures, and dump the error in the end. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | */ |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 518 | 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] | 519 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 520 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 521 | int res, ret = 0; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 522 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 523 | if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) { |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 524 | struct erase_info einfo; |
| 525 | |
| 526 | /* Attempt erase before marking OOB */ |
| 527 | memset(&einfo, 0, sizeof(einfo)); |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 528 | einfo.addr = ofs; |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 529 | einfo.len = 1ULL << chip->phys_erase_shift; |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 530 | nand_erase_nand(chip, &einfo, 0); |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 531 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 532 | /* Write bad block marker to OOB */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 533 | nand_get_device(mtd, FL_WRITING); |
Boris Brezillon | cdc784c | 2018-09-07 00:38:38 +0200 | [diff] [blame] | 534 | ret = nand_markbad_bbm(chip, ofs); |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 535 | nand_release_device(mtd); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 536 | } |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 537 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 538 | /* Mark block bad in BBT */ |
| 539 | if (chip->bbt) { |
Boris Brezillon | 5740d4c | 2018-09-06 14:05:34 +0200 | [diff] [blame] | 540 | res = nand_markbad_bbt(chip, ofs); |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 541 | if (!ret) |
| 542 | ret = res; |
| 543 | } |
| 544 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 545 | if (!ret) |
| 546 | mtd->ecc_stats.badblocks++; |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 547 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 548 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 551 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | * nand_check_wp - [GENERIC] check if the chip is write protected |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 553 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 555 | * Check, if the device is write protected. The function expects, that the |
| 556 | * device is already selected. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 558 | static int nand_check_wp(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 560 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 561 | u8 status; |
| 562 | int ret; |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 563 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 564 | /* Broken xD cards report WP despite being writable */ |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 565 | if (chip->options & NAND_BROKEN_XD) |
| 566 | return 0; |
| 567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | /* Check the WP bit */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 569 | ret = nand_status_op(chip, &status); |
| 570 | if (ret) |
| 571 | return ret; |
| 572 | |
| 573 | return status & NAND_STATUS_WP ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /** |
Gu Zheng | c30e1f7 | 2014-09-03 17:49:10 +0800 | [diff] [blame] | 577 | * nand_block_isreserved - [GENERIC] Check if a block is marked reserved. |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 578 | * @mtd: MTD device structure |
| 579 | * @ofs: offset from device start |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 580 | * |
Gu Zheng | c30e1f7 | 2014-09-03 17:49:10 +0800 | [diff] [blame] | 581 | * Check if the block is marked as reserved. |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 582 | */ |
| 583 | static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
| 584 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 585 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 586 | |
| 587 | if (!chip->bbt) |
| 588 | return 0; |
| 589 | /* Return info from the table */ |
Boris Brezillon | 5740d4c | 2018-09-06 14:05:34 +0200 | [diff] [blame] | 590 | return nand_isreserved_bbt(chip, ofs); |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /** |
| 594 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad |
| 595 | * @mtd: MTD device structure |
| 596 | * @ofs: offset from device start |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 597 | * @allowbbt: 1, if its allowed to access the bbt area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | * |
| 599 | * Check, if the block is bad. Either by reading the bad block table or |
| 600 | * calling of the scan function. |
| 601 | */ |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 602 | 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] | 603 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 604 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | /* Return info from the table */ |
Boris Brezillon | cdc784c | 2018-09-07 00:38:38 +0200 | [diff] [blame] | 607 | if (chip->bbt) |
| 608 | return nand_isbad_bbt(chip, ofs, allowbbt); |
| 609 | |
| 610 | return nand_isbad_bbm(chip, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 613 | /** |
| 614 | * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands. |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 615 | * @mtd: MTD device structure |
| 616 | * @timeo: Timeout |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 617 | * |
| 618 | * Helper function for nand_wait_ready used when needing to wait in interrupt |
| 619 | * context. |
| 620 | */ |
| 621 | static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo) |
| 622 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 623 | struct nand_chip *chip = mtd_to_nand(mtd); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 624 | int i; |
| 625 | |
| 626 | /* Wait for the device to get ready */ |
| 627 | for (i = 0; i < timeo; i++) { |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 628 | if (chip->legacy.dev_ready(chip)) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 629 | break; |
| 630 | touch_softlockup_watchdog(); |
| 631 | mdelay(1); |
| 632 | } |
| 633 | } |
| 634 | |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 635 | /** |
| 636 | * nand_wait_ready - [GENERIC] Wait for the ready pin after commands. |
Boris Brezillon | 2b356ab | 2018-09-06 14:05:16 +0200 | [diff] [blame] | 637 | * @chip: NAND chip object |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 638 | * |
| 639 | * Wait for the ready pin after a command, and warn if a timeout occurs. |
| 640 | */ |
Boris Brezillon | 2b356ab | 2018-09-06 14:05:16 +0200 | [diff] [blame] | 641 | void nand_wait_ready(struct nand_chip *chip) |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 642 | { |
Boris Brezillon | 2b356ab | 2018-09-06 14:05:16 +0200 | [diff] [blame] | 643 | struct mtd_info *mtd = nand_to_mtd(chip); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 644 | unsigned long timeo = 400; |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 645 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 646 | if (in_interrupt() || oops_in_progress) |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 647 | return panic_nand_wait_ready(mtd, timeo); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 648 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 649 | /* Wait until command is processed or timeout occurs */ |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 650 | timeo = jiffies + msecs_to_jiffies(timeo); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 651 | do { |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 652 | if (chip->legacy.dev_ready(chip)) |
Ezequiel Garcia | 4c7e054 | 2016-04-12 17:46:41 -0300 | [diff] [blame] | 653 | return; |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 654 | cond_resched(); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 655 | } while (time_before(jiffies, timeo)); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 656 | |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 657 | if (!chip->legacy.dev_ready(chip)) |
Brian Norris | 9ebfdf5 | 2016-03-04 17:19:23 -0800 | [diff] [blame] | 658 | pr_warn_ratelimited("timeout while waiting for chip to become ready\n"); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 659 | } |
David Woodhouse | 4b648b0 | 2006-09-25 17:05:24 +0100 | [diff] [blame] | 660 | EXPORT_SYMBOL_GPL(nand_wait_ready); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 661 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | /** |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 663 | * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands. |
| 664 | * @mtd: MTD device structure |
| 665 | * @timeo: Timeout in ms |
| 666 | * |
| 667 | * Wait for status ready (i.e. command done) or timeout. |
| 668 | */ |
| 669 | static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo) |
| 670 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 671 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 672 | int ret; |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 673 | |
| 674 | timeo = jiffies + msecs_to_jiffies(timeo); |
| 675 | do { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 676 | u8 status; |
| 677 | |
| 678 | ret = nand_read_data_op(chip, &status, sizeof(status), true); |
| 679 | if (ret) |
| 680 | return; |
| 681 | |
| 682 | if (status & NAND_STATUS_READY) |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 683 | break; |
| 684 | touch_softlockup_watchdog(); |
| 685 | } while (time_before(jiffies, timeo)); |
| 686 | }; |
| 687 | |
| 688 | /** |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 689 | * nand_soft_waitrdy - Poll STATUS reg until RDY bit is set to 1 |
| 690 | * @chip: NAND chip structure |
| 691 | * @timeout_ms: Timeout in ms |
| 692 | * |
| 693 | * Poll the STATUS register using ->exec_op() until the RDY bit becomes 1. |
| 694 | * If that does not happen whitin the specified timeout, -ETIMEDOUT is |
| 695 | * returned. |
| 696 | * |
| 697 | * This helper is intended to be used when the controller does not have access |
| 698 | * to the NAND R/B pin. |
| 699 | * |
| 700 | * Be aware that calling this helper from an ->exec_op() implementation means |
| 701 | * ->exec_op() must be re-entrant. |
| 702 | * |
| 703 | * Return 0 if the NAND chip is ready, a negative error otherwise. |
| 704 | */ |
| 705 | int nand_soft_waitrdy(struct nand_chip *chip, unsigned long timeout_ms) |
| 706 | { |
Boris Brezillon | 3057fce | 2018-05-04 21:24:31 +0200 | [diff] [blame] | 707 | const struct nand_sdr_timings *timings; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 708 | u8 status = 0; |
| 709 | int ret; |
| 710 | |
| 711 | if (!chip->exec_op) |
| 712 | return -ENOTSUPP; |
| 713 | |
Boris Brezillon | 3057fce | 2018-05-04 21:24:31 +0200 | [diff] [blame] | 714 | /* Wait tWB before polling the STATUS reg. */ |
| 715 | timings = nand_get_sdr_timings(&chip->data_interface); |
| 716 | ndelay(PSEC_TO_NSEC(timings->tWB_max)); |
| 717 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 718 | ret = nand_status_op(chip, NULL); |
| 719 | if (ret) |
| 720 | return ret; |
| 721 | |
| 722 | timeout_ms = jiffies + msecs_to_jiffies(timeout_ms); |
| 723 | do { |
| 724 | ret = nand_read_data_op(chip, &status, sizeof(status), true); |
| 725 | if (ret) |
| 726 | break; |
| 727 | |
| 728 | if (status & NAND_STATUS_READY) |
| 729 | break; |
| 730 | |
| 731 | /* |
| 732 | * Typical lowest execution time for a tR on most NANDs is 10us, |
| 733 | * use this as polling delay before doing something smarter (ie. |
| 734 | * deriving a delay from the timeout value, timeout_ms/ratio). |
| 735 | */ |
| 736 | udelay(10); |
| 737 | } while (time_before(jiffies, timeout_ms)); |
| 738 | |
| 739 | /* |
| 740 | * We have to exit READ_STATUS mode in order to read real data on the |
| 741 | * bus in case the WAITRDY instruction is preceding a DATA_IN |
| 742 | * instruction. |
| 743 | */ |
| 744 | nand_exit_status_op(chip); |
| 745 | |
| 746 | if (ret) |
| 747 | return ret; |
| 748 | |
| 749 | return status & NAND_STATUS_READY ? 0 : -ETIMEDOUT; |
| 750 | }; |
| 751 | EXPORT_SYMBOL_GPL(nand_soft_waitrdy); |
| 752 | |
| 753 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | * nand_command - [DEFAULT] Send command to NAND device |
Boris Brezillon | 5295cf2 | 2018-09-06 14:05:28 +0200 | [diff] [blame] | 755 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 756 | * @command: the command to be sent |
| 757 | * @column: the column address for this command, -1 if none |
| 758 | * @page_addr: the page address for this command, -1 if none |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 760 | * 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] | 761 | * (512 Bytes per page). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | */ |
Boris Brezillon | 5295cf2 | 2018-09-06 14:05:28 +0200 | [diff] [blame] | 763 | static void nand_command(struct nand_chip *chip, unsigned int command, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 764 | int column, int page_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | { |
Boris Brezillon | 5295cf2 | 2018-09-06 14:05:28 +0200 | [diff] [blame] | 766 | struct mtd_info *mtd = nand_to_mtd(chip); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 767 | int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 769 | /* Write out the command to the device */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | if (command == NAND_CMD_SEQIN) { |
| 771 | int readcmd; |
| 772 | |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 773 | if (column >= mtd->writesize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | /* OOB area */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 775 | column -= mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | readcmd = NAND_CMD_READOOB; |
| 777 | } else if (column < 256) { |
| 778 | /* First 256 bytes --> READ0 */ |
| 779 | readcmd = NAND_CMD_READ0; |
| 780 | } else { |
| 781 | column -= 256; |
| 782 | readcmd = NAND_CMD_READ1; |
| 783 | } |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 784 | chip->legacy.cmd_ctrl(chip, readcmd, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 785 | ctrl &= ~NAND_CTRL_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | } |
Miquel Raynal | df46789 | 2017-11-08 17:00:27 +0100 | [diff] [blame] | 787 | if (command != NAND_CMD_NONE) |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 788 | chip->legacy.cmd_ctrl(chip, command, ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 790 | /* Address cycle, when necessary */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 791 | ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE; |
| 792 | /* Serially input address */ |
| 793 | if (column != -1) { |
| 794 | /* Adjust columns for 16 bit buswidth */ |
Brian Norris | 3dad234 | 2014-01-29 14:08:12 -0800 | [diff] [blame] | 795 | if (chip->options & NAND_BUSWIDTH_16 && |
| 796 | !nand_opcode_8bits(command)) |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 797 | column >>= 1; |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 798 | chip->legacy.cmd_ctrl(chip, column, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 799 | ctrl &= ~NAND_CTRL_CHANGE; |
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 | if (page_addr != -1) { |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 802 | chip->legacy.cmd_ctrl(chip, page_addr, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 803 | ctrl &= ~NAND_CTRL_CHANGE; |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 804 | chip->legacy.cmd_ctrl(chip, page_addr >> 8, ctrl); |
Masahiro Yamada | 14157f8 | 2017-09-13 11:05:50 +0900 | [diff] [blame] | 805 | if (chip->options & NAND_ROW_ADDR_3) |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 806 | chip->legacy.cmd_ctrl(chip, page_addr >> 16, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 807 | } |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 808 | chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE, |
| 809 | NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 810 | |
| 811 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 812 | * Program and erase have their own busy handlers status and sequential |
| 813 | * in needs no delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 814 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | switch (command) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 816 | |
Miquel Raynal | df46789 | 2017-11-08 17:00:27 +0100 | [diff] [blame] | 817 | case NAND_CMD_NONE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | case NAND_CMD_PAGEPROG: |
| 819 | case NAND_CMD_ERASE1: |
| 820 | case NAND_CMD_ERASE2: |
| 821 | case NAND_CMD_SEQIN: |
| 822 | case NAND_CMD_STATUS: |
Masahiro Yamada | 3158fa0 | 2017-03-23 09:17:49 +0900 | [diff] [blame] | 823 | case NAND_CMD_READID: |
Masahiro Yamada | c5d664a | 2017-03-23 09:17:50 +0900 | [diff] [blame] | 824 | case NAND_CMD_SET_FEATURES: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | return; |
| 826 | |
| 827 | case NAND_CMD_RESET: |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 828 | if (chip->legacy.dev_ready) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | break; |
Boris Brezillon | 3cece3a | 2018-09-07 00:38:41 +0200 | [diff] [blame] | 830 | udelay(chip->legacy.chip_delay); |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 831 | chip->legacy.cmd_ctrl(chip, NAND_CMD_STATUS, |
| 832 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
| 833 | chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE, |
| 834 | NAND_NCE | NAND_CTRL_CHANGE); |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 835 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 836 | nand_wait_status_ready(mtd, 250); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | return; |
| 838 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 839 | /* This applies to read commands */ |
Boris Brezillon | 2165c4a | 2017-05-16 18:35:45 +0200 | [diff] [blame] | 840 | case NAND_CMD_READ0: |
| 841 | /* |
| 842 | * READ0 is sometimes used to exit GET STATUS mode. When this |
| 843 | * is the case no address cycles are requested, and we can use |
| 844 | * this information to detect that we should not wait for the |
| 845 | * device to be ready. |
| 846 | */ |
| 847 | if (column == -1 && page_addr == -1) |
| 848 | return; |
| 849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | default: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 851 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | * If we don't have access to the busy pin, we apply the given |
| 853 | * command delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 854 | */ |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 855 | if (!chip->legacy.dev_ready) { |
Boris Brezillon | 3cece3a | 2018-09-07 00:38:41 +0200 | [diff] [blame] | 856 | udelay(chip->legacy.chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | return; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 858 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 860 | /* |
| 861 | * Apply this short delay always to ensure that we do wait tWB in |
| 862 | * any case on any machine. |
| 863 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 864 | ndelay(100); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 865 | |
Boris Brezillon | 2b356ab | 2018-09-06 14:05:16 +0200 | [diff] [blame] | 866 | nand_wait_ready(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 869 | static void nand_ccs_delay(struct nand_chip *chip) |
| 870 | { |
| 871 | /* |
| 872 | * The controller already takes care of waiting for tCCS when the RNDIN |
| 873 | * or RNDOUT command is sent, return directly. |
| 874 | */ |
| 875 | if (!(chip->options & NAND_WAIT_TCCS)) |
| 876 | return; |
| 877 | |
| 878 | /* |
| 879 | * Wait tCCS_min if it is correctly defined, otherwise wait 500ns |
| 880 | * (which should be safe for all NANDs). |
| 881 | */ |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 882 | if (chip->setup_data_interface) |
| 883 | ndelay(chip->data_interface.timings.sdr.tCCS_min / 1000); |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 884 | else |
| 885 | ndelay(500); |
| 886 | } |
| 887 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | /** |
| 889 | * nand_command_lp - [DEFAULT] Send command to NAND large page device |
Boris Brezillon | 5295cf2 | 2018-09-06 14:05:28 +0200 | [diff] [blame] | 890 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 891 | * @command: the command to be sent |
| 892 | * @column: the column address for this command, -1 if none |
| 893 | * @page_addr: the page address for this command, -1 if none |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | * |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 895 | * 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] | 896 | * devices. We don't have the separate regions as we have in the small page |
| 897 | * devices. We must emulate NAND_CMD_READOOB to keep the code compatible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | */ |
Boris Brezillon | 5295cf2 | 2018-09-06 14:05:28 +0200 | [diff] [blame] | 899 | static void nand_command_lp(struct nand_chip *chip, unsigned int command, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 900 | int column, int page_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | { |
Boris Brezillon | 5295cf2 | 2018-09-06 14:05:28 +0200 | [diff] [blame] | 902 | struct mtd_info *mtd = nand_to_mtd(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
| 904 | /* Emulate NAND_CMD_READOOB */ |
| 905 | if (command == NAND_CMD_READOOB) { |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 906 | column += mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | command = NAND_CMD_READ0; |
| 908 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 909 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 910 | /* Command latch cycle */ |
Miquel Raynal | df46789 | 2017-11-08 17:00:27 +0100 | [diff] [blame] | 911 | if (command != NAND_CMD_NONE) |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 912 | chip->legacy.cmd_ctrl(chip, command, |
| 913 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | |
| 915 | if (column != -1 || page_addr != -1) { |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 916 | int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | |
| 918 | /* Serially input address */ |
| 919 | if (column != -1) { |
| 920 | /* Adjust columns for 16 bit buswidth */ |
Brian Norris | 3dad234 | 2014-01-29 14:08:12 -0800 | [diff] [blame] | 921 | if (chip->options & NAND_BUSWIDTH_16 && |
| 922 | !nand_opcode_8bits(command)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | column >>= 1; |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 924 | chip->legacy.cmd_ctrl(chip, column, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 925 | ctrl &= ~NAND_CTRL_CHANGE; |
Boris Brezillon | fde85cf | 2016-06-15 13:09:51 +0200 | [diff] [blame] | 926 | |
Brian Norris | f5b88de | 2016-10-03 09:49:35 -0700 | [diff] [blame] | 927 | /* Only output a single addr cycle for 8bits opcodes. */ |
Boris Brezillon | fde85cf | 2016-06-15 13:09:51 +0200 | [diff] [blame] | 928 | if (!nand_opcode_8bits(command)) |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 929 | chip->legacy.cmd_ctrl(chip, column >> 8, ctrl); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 930 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | if (page_addr != -1) { |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 932 | chip->legacy.cmd_ctrl(chip, page_addr, ctrl); |
| 933 | chip->legacy.cmd_ctrl(chip, page_addr >> 8, |
| 934 | NAND_NCE | NAND_ALE); |
Masahiro Yamada | 14157f8 | 2017-09-13 11:05:50 +0900 | [diff] [blame] | 935 | if (chip->options & NAND_ROW_ADDR_3) |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 936 | chip->legacy.cmd_ctrl(chip, page_addr >> 16, |
| 937 | NAND_NCE | NAND_ALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | } |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 940 | chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE, |
| 941 | NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 942 | |
| 943 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 944 | * Program and erase have their own busy handlers status, sequential |
Gerhard Sittig | 7a442f1 | 2014-03-29 14:36:22 +0100 | [diff] [blame] | 945 | * in and status need no delay. |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 946 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | switch (command) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 948 | |
Miquel Raynal | df46789 | 2017-11-08 17:00:27 +0100 | [diff] [blame] | 949 | case NAND_CMD_NONE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | case NAND_CMD_CACHEDPROG: |
| 951 | case NAND_CMD_PAGEPROG: |
| 952 | case NAND_CMD_ERASE1: |
| 953 | case NAND_CMD_ERASE2: |
| 954 | case NAND_CMD_SEQIN: |
| 955 | case NAND_CMD_STATUS: |
Masahiro Yamada | 3158fa0 | 2017-03-23 09:17:49 +0900 | [diff] [blame] | 956 | case NAND_CMD_READID: |
Masahiro Yamada | c5d664a | 2017-03-23 09:17:50 +0900 | [diff] [blame] | 957 | case NAND_CMD_SET_FEATURES: |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 958 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 960 | case NAND_CMD_RNDIN: |
| 961 | nand_ccs_delay(chip); |
| 962 | return; |
| 963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | case NAND_CMD_RESET: |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 965 | if (chip->legacy.dev_ready) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | break; |
Boris Brezillon | 3cece3a | 2018-09-07 00:38:41 +0200 | [diff] [blame] | 967 | udelay(chip->legacy.chip_delay); |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 968 | chip->legacy.cmd_ctrl(chip, NAND_CMD_STATUS, |
| 969 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 970 | chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE, |
| 971 | NAND_NCE | NAND_CTRL_CHANGE); |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 972 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 973 | nand_wait_status_ready(mtd, 250); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | return; |
| 975 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 976 | case NAND_CMD_RNDOUT: |
| 977 | /* No ready / busy check necessary */ |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 978 | chip->legacy.cmd_ctrl(chip, NAND_CMD_RNDOUTSTART, |
| 979 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 980 | chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE, |
| 981 | NAND_NCE | NAND_CTRL_CHANGE); |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 982 | |
| 983 | nand_ccs_delay(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 984 | return; |
| 985 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | case NAND_CMD_READ0: |
Boris Brezillon | 2165c4a | 2017-05-16 18:35:45 +0200 | [diff] [blame] | 987 | /* |
| 988 | * READ0 is sometimes used to exit GET STATUS mode. When this |
| 989 | * is the case no address cycles are requested, and we can use |
| 990 | * this information to detect that READSTART should not be |
| 991 | * issued. |
| 992 | */ |
| 993 | if (column == -1 && page_addr == -1) |
| 994 | return; |
| 995 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 996 | chip->legacy.cmd_ctrl(chip, NAND_CMD_READSTART, |
| 997 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 998 | chip->legacy.cmd_ctrl(chip, NAND_CMD_NONE, |
| 999 | NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1000 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1001 | /* This applies to read commands */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | default: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1003 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | * 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] | 1005 | * command delay. |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1006 | */ |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1007 | if (!chip->legacy.dev_ready) { |
Boris Brezillon | 3cece3a | 2018-09-07 00:38:41 +0200 | [diff] [blame] | 1008 | udelay(chip->legacy.chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | return; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1010 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | } |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 1012 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1013 | /* |
| 1014 | * Apply this short delay always to ensure that we do wait tWB in |
| 1015 | * any case on any machine. |
| 1016 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1017 | ndelay(100); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 1018 | |
Boris Brezillon | 2b356ab | 2018-09-06 14:05:16 +0200 | [diff] [blame] | 1019 | nand_wait_ready(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1023 | * panic_nand_get_device - [GENERIC] Get chip for selected access |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1024 | * @chip: the nand chip descriptor |
| 1025 | * @mtd: MTD device structure |
| 1026 | * @new_state: the state which is requested |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1027 | * |
| 1028 | * Used when in panic, no locks are taken. |
| 1029 | */ |
| 1030 | static void panic_nand_get_device(struct nand_chip *chip, |
| 1031 | struct mtd_info *mtd, int new_state) |
| 1032 | { |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1033 | /* Hardware controller shared among independent devices */ |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1034 | chip->controller->active = chip; |
| 1035 | chip->state = new_state; |
| 1036 | } |
| 1037 | |
| 1038 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | * nand_get_device - [GENERIC] Get chip for selected access |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1040 | * @mtd: MTD device structure |
| 1041 | * @new_state: the state which is requested |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | * |
| 1043 | * Get the device and lock it for exclusive access |
| 1044 | */ |
Thomas Gleixner | 2c0a2be | 2006-05-23 11:50:56 +0200 | [diff] [blame] | 1045 | static int |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 1046 | nand_get_device(struct mtd_info *mtd, int new_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 1048 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1049 | spinlock_t *lock = &chip->controller->lock; |
| 1050 | wait_queue_head_t *wq = &chip->controller->wq; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1051 | DECLARE_WAITQUEUE(wait, current); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1052 | retry: |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 1053 | spin_lock(lock); |
| 1054 | |
vimal singh | b8b3ee9 | 2009-07-09 20:41:22 +0530 | [diff] [blame] | 1055 | /* Hardware controller shared among independent devices */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1056 | if (!chip->controller->active) |
| 1057 | chip->controller->active = chip; |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 1058 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1059 | if (chip->controller->active == chip && chip->state == FL_READY) { |
| 1060 | chip->state = new_state; |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 1061 | spin_unlock(lock); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 1062 | return 0; |
| 1063 | } |
| 1064 | if (new_state == FL_PM_SUSPENDED) { |
Li Yang | 6b0d9a8 | 2009-11-17 14:45:49 -0800 | [diff] [blame] | 1065 | if (chip->controller->active->state == FL_PM_SUSPENDED) { |
| 1066 | chip->state = FL_PM_SUSPENDED; |
| 1067 | spin_unlock(lock); |
| 1068 | return 0; |
Li Yang | 6b0d9a8 | 2009-11-17 14:45:49 -0800 | [diff] [blame] | 1069 | } |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 1070 | } |
| 1071 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 1072 | add_wait_queue(wq, &wait); |
| 1073 | spin_unlock(lock); |
| 1074 | schedule(); |
| 1075 | remove_wait_queue(wq, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | goto retry; |
| 1077 | } |
| 1078 | |
| 1079 | /** |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1080 | * panic_nand_wait - [GENERIC] wait until the command is done |
| 1081 | * @mtd: MTD device structure |
| 1082 | * @chip: NAND chip structure |
| 1083 | * @timeo: timeout |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1084 | * |
| 1085 | * Wait for command done. This is a helper function for nand_wait used when |
| 1086 | * 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] | 1087 | * an oops through mtdoops. |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1088 | */ |
Boris Brezillon | f1d4694 | 2018-09-06 14:05:29 +0200 | [diff] [blame] | 1089 | static void panic_nand_wait(struct nand_chip *chip, unsigned long timeo) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1090 | { |
| 1091 | int i; |
| 1092 | for (i = 0; i < timeo; i++) { |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1093 | if (chip->legacy.dev_ready) { |
| 1094 | if (chip->legacy.dev_ready(chip)) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1095 | break; |
| 1096 | } else { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1097 | int ret; |
| 1098 | u8 status; |
| 1099 | |
| 1100 | ret = nand_read_data_op(chip, &status, sizeof(status), |
| 1101 | true); |
| 1102 | if (ret) |
| 1103 | return; |
| 1104 | |
| 1105 | if (status & NAND_STATUS_READY) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1106 | break; |
| 1107 | } |
| 1108 | mdelay(1); |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 1109 | } |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | /** |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1113 | * nand_wait - [DEFAULT] wait until the command is done |
| 1114 | * @mtd: MTD device structure |
| 1115 | * @chip: NAND chip structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | * |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1117 | * Wait for command done. This applies to erase and program only. |
Randy Dunlap | 844d3b4 | 2006-06-28 21:48:27 -0700 | [diff] [blame] | 1118 | */ |
Boris Brezillon | f1d4694 | 2018-09-06 14:05:29 +0200 | [diff] [blame] | 1119 | static int nand_wait(struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | { |
| 1121 | |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1122 | unsigned long timeo = 400; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1123 | u8 status; |
| 1124 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1126 | /* |
| 1127 | * Apply this short delay always to ensure that we do wait tWB in any |
| 1128 | * case on any machine. |
| 1129 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1130 | ndelay(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1132 | ret = nand_status_op(chip, NULL); |
| 1133 | if (ret) |
| 1134 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1136 | if (in_interrupt() || oops_in_progress) |
Boris Brezillon | f1d4694 | 2018-09-06 14:05:29 +0200 | [diff] [blame] | 1137 | panic_nand_wait(chip, timeo); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1138 | else { |
Huang Shijie | 6d2559f | 2013-01-30 10:03:56 +0800 | [diff] [blame] | 1139 | timeo = jiffies + msecs_to_jiffies(timeo); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1140 | do { |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1141 | if (chip->legacy.dev_ready) { |
| 1142 | if (chip->legacy.dev_ready(chip)) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1143 | break; |
| 1144 | } else { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1145 | ret = nand_read_data_op(chip, &status, |
| 1146 | sizeof(status), true); |
| 1147 | if (ret) |
| 1148 | return ret; |
| 1149 | |
| 1150 | if (status & NAND_STATUS_READY) |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 1151 | break; |
| 1152 | } |
| 1153 | cond_resched(); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 1154 | } while (time_before(jiffies, timeo)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | } |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 1156 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1157 | ret = nand_read_data_op(chip, &status, sizeof(status), true); |
| 1158 | if (ret) |
| 1159 | return ret; |
| 1160 | |
Matthieu CASTET | f251b8d | 2012-11-05 15:00:44 +0100 | [diff] [blame] | 1161 | /* This can happen if in case of timeout or buggy dev_ready */ |
| 1162 | WARN_ON(!(status & NAND_STATUS_READY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | return status; |
| 1164 | } |
| 1165 | |
Miquel Raynal | 789157e | 2018-03-19 14:47:28 +0100 | [diff] [blame] | 1166 | static bool nand_supports_get_features(struct nand_chip *chip, int addr) |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 1167 | { |
Miquel Raynal | 789157e | 2018-03-19 14:47:28 +0100 | [diff] [blame] | 1168 | return (chip->parameters.supports_set_get_features && |
| 1169 | test_bit(addr, chip->parameters.get_feature_list)); |
| 1170 | } |
| 1171 | |
| 1172 | static bool nand_supports_set_features(struct nand_chip *chip, int addr) |
| 1173 | { |
| 1174 | return (chip->parameters.supports_set_get_features && |
| 1175 | test_bit(addr, chip->parameters.set_feature_list)); |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | /** |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1179 | * nand_reset_data_interface - Reset data interface and timings |
| 1180 | * @chip: The NAND chip |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1181 | * @chipnr: Internal die id |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1182 | * |
| 1183 | * Reset the Data interface and timings to ONFI mode 0. |
| 1184 | * |
| 1185 | * Returns 0 for success or negative error code otherwise. |
| 1186 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1187 | static int nand_reset_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1188 | { |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1189 | int ret; |
| 1190 | |
| 1191 | if (!chip->setup_data_interface) |
| 1192 | return 0; |
| 1193 | |
| 1194 | /* |
| 1195 | * The ONFI specification says: |
| 1196 | * " |
| 1197 | * To transition from NV-DDR or NV-DDR2 to the SDR data |
| 1198 | * interface, the host shall use the Reset (FFh) command |
| 1199 | * using SDR timing mode 0. A device in any timing mode is |
| 1200 | * required to recognize Reset (FFh) command issued in SDR |
| 1201 | * timing mode 0. |
| 1202 | * " |
| 1203 | * |
| 1204 | * Configure the data interface in SDR mode and set the |
| 1205 | * timings to timing mode 0. |
| 1206 | */ |
| 1207 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 1208 | onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0); |
Boris Brezillon | 858838b | 2018-09-06 14:05:33 +0200 | [diff] [blame] | 1209 | ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1210 | if (ret) |
| 1211 | pr_err("Failed to configure data interface to SDR timing mode 0\n"); |
| 1212 | |
| 1213 | return ret; |
| 1214 | } |
| 1215 | |
| 1216 | /** |
| 1217 | * nand_setup_data_interface - Setup the best data interface and timings |
| 1218 | * @chip: The NAND chip |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1219 | * @chipnr: Internal die id |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1220 | * |
| 1221 | * Find and configure the best data interface and NAND timings supported by |
| 1222 | * the chip and the driver. |
| 1223 | * First tries to retrieve supported timing modes from ONFI information, |
| 1224 | * and if the NAND chip does not support ONFI, relies on the |
| 1225 | * ->onfi_timing_mode_default specified in the nand_ids table. |
| 1226 | * |
| 1227 | * Returns 0 for success or negative error code otherwise. |
| 1228 | */ |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1229 | static int nand_setup_data_interface(struct nand_chip *chip, int chipnr) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1230 | { |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 1231 | u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = { |
| 1232 | chip->onfi_timing_mode_default, |
| 1233 | }; |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1234 | int ret; |
| 1235 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 1236 | if (!chip->setup_data_interface) |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1237 | return 0; |
| 1238 | |
Miquel Raynal | 993447b | 2018-03-19 14:47:21 +0100 | [diff] [blame] | 1239 | /* Change the mode on the chip side (if supported by the NAND chip) */ |
Miquel Raynal | 789157e | 2018-03-19 14:47:28 +0100 | [diff] [blame] | 1240 | if (nand_supports_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) { |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 1241 | chip->select_chip(chip, chipnr); |
Miquel Raynal | 993447b | 2018-03-19 14:47:21 +0100 | [diff] [blame] | 1242 | ret = nand_set_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE, |
| 1243 | tmode_param); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 1244 | chip->select_chip(chip, -1); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1245 | if (ret) |
Miquel Raynal | 993447b | 2018-03-19 14:47:21 +0100 | [diff] [blame] | 1246 | return ret; |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1247 | } |
| 1248 | |
Miquel Raynal | 97baea1 | 2018-03-19 14:47:20 +0100 | [diff] [blame] | 1249 | /* Change the mode on the controller side */ |
Boris Brezillon | 858838b | 2018-09-06 14:05:33 +0200 | [diff] [blame] | 1250 | ret = chip->setup_data_interface(chip, chipnr, &chip->data_interface); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 1251 | if (ret) |
| 1252 | return ret; |
| 1253 | |
| 1254 | /* Check the mode has been accepted by the chip, if supported */ |
Miquel Raynal | 789157e | 2018-03-19 14:47:28 +0100 | [diff] [blame] | 1255 | if (!nand_supports_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE)) |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 1256 | return 0; |
| 1257 | |
| 1258 | memset(tmode_param, 0, ONFI_SUBFEATURE_PARAM_LEN); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 1259 | chip->select_chip(chip, chipnr); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 1260 | ret = nand_get_features(chip, ONFI_FEATURE_ADDR_TIMING_MODE, |
| 1261 | tmode_param); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 1262 | chip->select_chip(chip, -1); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 1263 | if (ret) |
| 1264 | goto err_reset_chip; |
| 1265 | |
| 1266 | if (tmode_param[0] != chip->onfi_timing_mode_default) { |
| 1267 | pr_warn("timing mode %d not acknowledged by the NAND chip\n", |
| 1268 | chip->onfi_timing_mode_default); |
| 1269 | goto err_reset_chip; |
| 1270 | } |
| 1271 | |
| 1272 | return 0; |
| 1273 | |
| 1274 | err_reset_chip: |
| 1275 | /* |
| 1276 | * Fallback to mode 0 if the chip explicitly did not ack the chosen |
| 1277 | * timing mode. |
| 1278 | */ |
| 1279 | nand_reset_data_interface(chip, chipnr); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 1280 | chip->select_chip(chip, chipnr); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 1281 | nand_reset_op(chip); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 1282 | chip->select_chip(chip, -1); |
Miquel Raynal | 415ae78 | 2018-03-19 14:47:24 +0100 | [diff] [blame] | 1283 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1284 | return ret; |
| 1285 | } |
| 1286 | |
| 1287 | /** |
| 1288 | * nand_init_data_interface - find the best data interface and timings |
| 1289 | * @chip: The NAND chip |
| 1290 | * |
| 1291 | * Find the best data interface and NAND timings supported by the chip |
| 1292 | * and the driver. |
| 1293 | * First tries to retrieve supported timing modes from ONFI information, |
| 1294 | * and if the NAND chip does not support ONFI, relies on the |
| 1295 | * ->onfi_timing_mode_default specified in the nand_ids table. After this |
| 1296 | * function nand_chip->data_interface is initialized with the best timing mode |
| 1297 | * available. |
| 1298 | * |
| 1299 | * Returns 0 for success or negative error code otherwise. |
| 1300 | */ |
| 1301 | static int nand_init_data_interface(struct nand_chip *chip) |
| 1302 | { |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1303 | int modes, mode, ret; |
| 1304 | |
| 1305 | if (!chip->setup_data_interface) |
| 1306 | return 0; |
| 1307 | |
| 1308 | /* |
| 1309 | * First try to identify the best timings from ONFI parameters and |
| 1310 | * if the NAND does not support ONFI, fallback to the default ONFI |
| 1311 | * timing mode. |
| 1312 | */ |
| 1313 | modes = onfi_get_async_timing_mode(chip); |
| 1314 | if (modes == ONFI_TIMING_MODE_UNKNOWN) { |
| 1315 | if (!chip->onfi_timing_mode_default) |
| 1316 | return 0; |
| 1317 | |
| 1318 | modes = GENMASK(chip->onfi_timing_mode_default, 0); |
| 1319 | } |
| 1320 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1321 | |
| 1322 | for (mode = fls(modes) - 1; mode >= 0; mode--) { |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 1323 | ret = onfi_fill_data_interface(chip, NAND_SDR_IFACE, mode); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1324 | if (ret) |
| 1325 | continue; |
| 1326 | |
Miquel Raynal | d787b8b | 2017-12-22 18:12:41 +0100 | [diff] [blame] | 1327 | /* |
| 1328 | * Pass NAND_DATA_IFACE_CHECK_ONLY to only check if the |
| 1329 | * controller supports the requested timings. |
| 1330 | */ |
Boris Brezillon | 858838b | 2018-09-06 14:05:33 +0200 | [diff] [blame] | 1331 | ret = chip->setup_data_interface(chip, |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 1332 | NAND_DATA_IFACE_CHECK_ONLY, |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 1333 | &chip->data_interface); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1334 | if (!ret) { |
| 1335 | chip->onfi_timing_mode_default = mode; |
| 1336 | break; |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1343 | /** |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1344 | * nand_fill_column_cycles - fill the column cycles of an address |
| 1345 | * @chip: The NAND chip |
| 1346 | * @addrs: Array of address cycles to fill |
| 1347 | * @offset_in_page: The offset in the page |
| 1348 | * |
| 1349 | * Fills the first or the first two bytes of the @addrs field depending |
| 1350 | * on the NAND bus width and the page size. |
| 1351 | * |
| 1352 | * Returns the number of cycles needed to encode the column, or a negative |
| 1353 | * error code in case one of the arguments is invalid. |
| 1354 | */ |
| 1355 | static int nand_fill_column_cycles(struct nand_chip *chip, u8 *addrs, |
| 1356 | unsigned int offset_in_page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1358 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1359 | |
| 1360 | /* Make sure the offset is less than the actual page size. */ |
| 1361 | if (offset_in_page > mtd->writesize + mtd->oobsize) |
| 1362 | return -EINVAL; |
| 1363 | |
| 1364 | /* |
| 1365 | * On small page NANDs, there's a dedicated command to access the OOB |
| 1366 | * area, and the column address is relative to the start of the OOB |
| 1367 | * area, not the start of the page. Asjust the address accordingly. |
| 1368 | */ |
| 1369 | if (mtd->writesize <= 512 && offset_in_page >= mtd->writesize) |
| 1370 | offset_in_page -= mtd->writesize; |
| 1371 | |
| 1372 | /* |
| 1373 | * The offset in page is expressed in bytes, if the NAND bus is 16-bit |
| 1374 | * wide, then it must be divided by 2. |
| 1375 | */ |
| 1376 | if (chip->options & NAND_BUSWIDTH_16) { |
| 1377 | if (WARN_ON(offset_in_page % 2)) |
| 1378 | return -EINVAL; |
| 1379 | |
| 1380 | offset_in_page /= 2; |
| 1381 | } |
| 1382 | |
| 1383 | addrs[0] = offset_in_page; |
| 1384 | |
| 1385 | /* |
| 1386 | * Small page NANDs use 1 cycle for the columns, while large page NANDs |
| 1387 | * need 2 |
| 1388 | */ |
| 1389 | if (mtd->writesize <= 512) |
| 1390 | return 1; |
| 1391 | |
| 1392 | addrs[1] = offset_in_page >> 8; |
| 1393 | |
| 1394 | return 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | } |
| 1396 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1397 | static int nand_sp_exec_read_page_op(struct nand_chip *chip, unsigned int page, |
| 1398 | unsigned int offset_in_page, void *buf, |
| 1399 | unsigned int len) |
| 1400 | { |
| 1401 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1402 | const struct nand_sdr_timings *sdr = |
| 1403 | nand_get_sdr_timings(&chip->data_interface); |
| 1404 | u8 addrs[4]; |
| 1405 | struct nand_op_instr instrs[] = { |
| 1406 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 1407 | NAND_OP_ADDR(3, addrs, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1408 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max), |
| 1409 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 1410 | NAND_OP_DATA_IN(len, buf, 0), |
| 1411 | }; |
| 1412 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1413 | int ret; |
| 1414 | |
| 1415 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1416 | if (!len) |
| 1417 | op.ninstrs--; |
| 1418 | |
| 1419 | if (offset_in_page >= mtd->writesize) |
| 1420 | instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB; |
| 1421 | else if (offset_in_page >= 256 && |
| 1422 | !(chip->options & NAND_BUSWIDTH_16)) |
| 1423 | instrs[0].ctx.cmd.opcode = NAND_CMD_READ1; |
| 1424 | |
| 1425 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1426 | if (ret < 0) |
| 1427 | return ret; |
| 1428 | |
| 1429 | addrs[1] = page; |
| 1430 | addrs[2] = page >> 8; |
| 1431 | |
| 1432 | if (chip->options & NAND_ROW_ADDR_3) { |
| 1433 | addrs[3] = page >> 16; |
| 1434 | instrs[1].ctx.addr.naddrs++; |
| 1435 | } |
| 1436 | |
| 1437 | return nand_exec_op(chip, &op); |
| 1438 | } |
| 1439 | |
| 1440 | static int nand_lp_exec_read_page_op(struct nand_chip *chip, unsigned int page, |
| 1441 | unsigned int offset_in_page, void *buf, |
| 1442 | unsigned int len) |
| 1443 | { |
| 1444 | const struct nand_sdr_timings *sdr = |
| 1445 | nand_get_sdr_timings(&chip->data_interface); |
| 1446 | u8 addrs[5]; |
| 1447 | struct nand_op_instr instrs[] = { |
| 1448 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 1449 | NAND_OP_ADDR(4, addrs, 0), |
| 1450 | NAND_OP_CMD(NAND_CMD_READSTART, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1451 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max), |
| 1452 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 1453 | NAND_OP_DATA_IN(len, buf, 0), |
| 1454 | }; |
| 1455 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1456 | int ret; |
| 1457 | |
| 1458 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1459 | if (!len) |
| 1460 | op.ninstrs--; |
| 1461 | |
| 1462 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1463 | if (ret < 0) |
| 1464 | return ret; |
| 1465 | |
| 1466 | addrs[2] = page; |
| 1467 | addrs[3] = page >> 8; |
| 1468 | |
| 1469 | if (chip->options & NAND_ROW_ADDR_3) { |
| 1470 | addrs[4] = page >> 16; |
| 1471 | instrs[1].ctx.addr.naddrs++; |
| 1472 | } |
| 1473 | |
| 1474 | return nand_exec_op(chip, &op); |
| 1475 | } |
| 1476 | |
| 1477 | /** |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1478 | * nand_read_page_op - Do a READ PAGE operation |
| 1479 | * @chip: The NAND chip |
| 1480 | * @page: page to read |
| 1481 | * @offset_in_page: offset within the page |
| 1482 | * @buf: buffer used to store the data |
| 1483 | * @len: length of the buffer |
| 1484 | * |
| 1485 | * This function issues a READ PAGE operation. |
| 1486 | * This function does not select/unselect the CS line. |
| 1487 | * |
| 1488 | * Returns 0 on success, a negative error code otherwise. |
| 1489 | */ |
| 1490 | int nand_read_page_op(struct nand_chip *chip, unsigned int page, |
| 1491 | unsigned int offset_in_page, void *buf, unsigned int len) |
| 1492 | { |
| 1493 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1494 | |
| 1495 | if (len && !buf) |
| 1496 | return -EINVAL; |
| 1497 | |
| 1498 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1499 | return -EINVAL; |
| 1500 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1501 | if (chip->exec_op) { |
| 1502 | if (mtd->writesize > 512) |
| 1503 | return nand_lp_exec_read_page_op(chip, page, |
| 1504 | offset_in_page, buf, |
| 1505 | len); |
| 1506 | |
| 1507 | return nand_sp_exec_read_page_op(chip, page, offset_in_page, |
| 1508 | buf, len); |
| 1509 | } |
| 1510 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1511 | chip->legacy.cmdfunc(chip, NAND_CMD_READ0, offset_in_page, page); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1512 | if (len) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1513 | chip->legacy.read_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1514 | |
| 1515 | return 0; |
| 1516 | } |
| 1517 | EXPORT_SYMBOL_GPL(nand_read_page_op); |
| 1518 | |
| 1519 | /** |
| 1520 | * nand_read_param_page_op - Do a READ PARAMETER PAGE operation |
| 1521 | * @chip: The NAND chip |
| 1522 | * @page: parameter page to read |
| 1523 | * @buf: buffer used to store the data |
| 1524 | * @len: length of the buffer |
| 1525 | * |
| 1526 | * This function issues a READ PARAMETER PAGE operation. |
| 1527 | * This function does not select/unselect the CS line. |
| 1528 | * |
| 1529 | * Returns 0 on success, a negative error code otherwise. |
| 1530 | */ |
| 1531 | static int nand_read_param_page_op(struct nand_chip *chip, u8 page, void *buf, |
| 1532 | unsigned int len) |
| 1533 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1534 | unsigned int i; |
| 1535 | u8 *p = buf; |
| 1536 | |
| 1537 | if (len && !buf) |
| 1538 | return -EINVAL; |
| 1539 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1540 | if (chip->exec_op) { |
| 1541 | const struct nand_sdr_timings *sdr = |
| 1542 | nand_get_sdr_timings(&chip->data_interface); |
| 1543 | struct nand_op_instr instrs[] = { |
| 1544 | NAND_OP_CMD(NAND_CMD_PARAM, 0), |
| 1545 | NAND_OP_ADDR(1, &page, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1546 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tR_max), |
| 1547 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 1548 | NAND_OP_8BIT_DATA_IN(len, buf, 0), |
| 1549 | }; |
| 1550 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1551 | |
| 1552 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1553 | if (!len) |
| 1554 | op.ninstrs--; |
| 1555 | |
| 1556 | return nand_exec_op(chip, &op); |
| 1557 | } |
| 1558 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1559 | chip->legacy.cmdfunc(chip, NAND_CMD_PARAM, page, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1560 | for (i = 0; i < len; i++) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1561 | p[i] = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1562 | |
| 1563 | return 0; |
| 1564 | } |
| 1565 | |
| 1566 | /** |
| 1567 | * nand_change_read_column_op - Do a CHANGE READ COLUMN operation |
| 1568 | * @chip: The NAND chip |
| 1569 | * @offset_in_page: offset within the page |
| 1570 | * @buf: buffer used to store the data |
| 1571 | * @len: length of the buffer |
| 1572 | * @force_8bit: force 8-bit bus access |
| 1573 | * |
| 1574 | * This function issues a CHANGE READ COLUMN operation. |
| 1575 | * This function does not select/unselect the CS line. |
| 1576 | * |
| 1577 | * Returns 0 on success, a negative error code otherwise. |
| 1578 | */ |
| 1579 | int nand_change_read_column_op(struct nand_chip *chip, |
| 1580 | unsigned int offset_in_page, void *buf, |
| 1581 | unsigned int len, bool force_8bit) |
| 1582 | { |
| 1583 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1584 | |
| 1585 | if (len && !buf) |
| 1586 | return -EINVAL; |
| 1587 | |
| 1588 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1589 | return -EINVAL; |
| 1590 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1591 | /* Small page NANDs do not support column change. */ |
| 1592 | if (mtd->writesize <= 512) |
| 1593 | return -ENOTSUPP; |
| 1594 | |
| 1595 | if (chip->exec_op) { |
| 1596 | const struct nand_sdr_timings *sdr = |
| 1597 | nand_get_sdr_timings(&chip->data_interface); |
| 1598 | u8 addrs[2] = {}; |
| 1599 | struct nand_op_instr instrs[] = { |
| 1600 | NAND_OP_CMD(NAND_CMD_RNDOUT, 0), |
| 1601 | NAND_OP_ADDR(2, addrs, 0), |
| 1602 | NAND_OP_CMD(NAND_CMD_RNDOUTSTART, |
| 1603 | PSEC_TO_NSEC(sdr->tCCS_min)), |
| 1604 | NAND_OP_DATA_IN(len, buf, 0), |
| 1605 | }; |
| 1606 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1607 | int ret; |
| 1608 | |
| 1609 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1610 | if (ret < 0) |
| 1611 | return ret; |
| 1612 | |
| 1613 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1614 | if (!len) |
| 1615 | op.ninstrs--; |
| 1616 | |
| 1617 | instrs[3].ctx.data.force_8bit = force_8bit; |
| 1618 | |
| 1619 | return nand_exec_op(chip, &op); |
| 1620 | } |
| 1621 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1622 | chip->legacy.cmdfunc(chip, NAND_CMD_RNDOUT, offset_in_page, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1623 | if (len) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1624 | chip->legacy.read_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1625 | |
| 1626 | return 0; |
| 1627 | } |
| 1628 | EXPORT_SYMBOL_GPL(nand_change_read_column_op); |
| 1629 | |
| 1630 | /** |
| 1631 | * nand_read_oob_op - Do a READ OOB operation |
| 1632 | * @chip: The NAND chip |
| 1633 | * @page: page to read |
| 1634 | * @offset_in_oob: offset within the OOB area |
| 1635 | * @buf: buffer used to store the data |
| 1636 | * @len: length of the buffer |
| 1637 | * |
| 1638 | * This function issues a READ OOB operation. |
| 1639 | * This function does not select/unselect the CS line. |
| 1640 | * |
| 1641 | * Returns 0 on success, a negative error code otherwise. |
| 1642 | */ |
| 1643 | int nand_read_oob_op(struct nand_chip *chip, unsigned int page, |
| 1644 | unsigned int offset_in_oob, void *buf, unsigned int len) |
| 1645 | { |
| 1646 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1647 | |
| 1648 | if (len && !buf) |
| 1649 | return -EINVAL; |
| 1650 | |
| 1651 | if (offset_in_oob + len > mtd->oobsize) |
| 1652 | return -EINVAL; |
| 1653 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1654 | if (chip->exec_op) |
| 1655 | return nand_read_page_op(chip, page, |
| 1656 | mtd->writesize + offset_in_oob, |
| 1657 | buf, len); |
| 1658 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1659 | chip->legacy.cmdfunc(chip, NAND_CMD_READOOB, offset_in_oob, page); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1660 | if (len) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1661 | chip->legacy.read_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1662 | |
| 1663 | return 0; |
| 1664 | } |
| 1665 | EXPORT_SYMBOL_GPL(nand_read_oob_op); |
| 1666 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1667 | static int nand_exec_prog_page_op(struct nand_chip *chip, unsigned int page, |
| 1668 | unsigned int offset_in_page, const void *buf, |
| 1669 | unsigned int len, bool prog) |
| 1670 | { |
| 1671 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1672 | const struct nand_sdr_timings *sdr = |
| 1673 | nand_get_sdr_timings(&chip->data_interface); |
| 1674 | u8 addrs[5] = {}; |
| 1675 | struct nand_op_instr instrs[] = { |
| 1676 | /* |
| 1677 | * The first instruction will be dropped if we're dealing |
| 1678 | * with a large page NAND and adjusted if we're dealing |
| 1679 | * with a small page NAND and the page offset is > 255. |
| 1680 | */ |
| 1681 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 1682 | NAND_OP_CMD(NAND_CMD_SEQIN, 0), |
| 1683 | NAND_OP_ADDR(0, addrs, PSEC_TO_NSEC(sdr->tADL_min)), |
| 1684 | NAND_OP_DATA_OUT(len, buf, 0), |
| 1685 | NAND_OP_CMD(NAND_CMD_PAGEPROG, PSEC_TO_NSEC(sdr->tWB_max)), |
| 1686 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0), |
| 1687 | }; |
| 1688 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1689 | int naddrs = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1690 | int ret; |
| 1691 | u8 status; |
| 1692 | |
| 1693 | if (naddrs < 0) |
| 1694 | return naddrs; |
| 1695 | |
| 1696 | addrs[naddrs++] = page; |
| 1697 | addrs[naddrs++] = page >> 8; |
| 1698 | if (chip->options & NAND_ROW_ADDR_3) |
| 1699 | addrs[naddrs++] = page >> 16; |
| 1700 | |
| 1701 | instrs[2].ctx.addr.naddrs = naddrs; |
| 1702 | |
| 1703 | /* Drop the last two instructions if we're not programming the page. */ |
| 1704 | if (!prog) { |
| 1705 | op.ninstrs -= 2; |
| 1706 | /* Also drop the DATA_OUT instruction if empty. */ |
| 1707 | if (!len) |
| 1708 | op.ninstrs--; |
| 1709 | } |
| 1710 | |
| 1711 | if (mtd->writesize <= 512) { |
| 1712 | /* |
| 1713 | * Small pages need some more tweaking: we have to adjust the |
| 1714 | * first instruction depending on the page offset we're trying |
| 1715 | * to access. |
| 1716 | */ |
| 1717 | if (offset_in_page >= mtd->writesize) |
| 1718 | instrs[0].ctx.cmd.opcode = NAND_CMD_READOOB; |
| 1719 | else if (offset_in_page >= 256 && |
| 1720 | !(chip->options & NAND_BUSWIDTH_16)) |
| 1721 | instrs[0].ctx.cmd.opcode = NAND_CMD_READ1; |
| 1722 | } else { |
| 1723 | /* |
| 1724 | * Drop the first command if we're dealing with a large page |
| 1725 | * NAND. |
| 1726 | */ |
| 1727 | op.instrs++; |
| 1728 | op.ninstrs--; |
| 1729 | } |
| 1730 | |
| 1731 | ret = nand_exec_op(chip, &op); |
| 1732 | if (!prog || ret) |
| 1733 | return ret; |
| 1734 | |
| 1735 | ret = nand_status_op(chip, &status); |
| 1736 | if (ret) |
| 1737 | return ret; |
| 1738 | |
| 1739 | return status; |
| 1740 | } |
| 1741 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1742 | /** |
| 1743 | * nand_prog_page_begin_op - starts a PROG PAGE operation |
| 1744 | * @chip: The NAND chip |
| 1745 | * @page: page to write |
| 1746 | * @offset_in_page: offset within the page |
| 1747 | * @buf: buffer containing the data to write to the page |
| 1748 | * @len: length of the buffer |
| 1749 | * |
| 1750 | * This function issues the first half of a PROG PAGE operation. |
| 1751 | * This function does not select/unselect the CS line. |
| 1752 | * |
| 1753 | * Returns 0 on success, a negative error code otherwise. |
| 1754 | */ |
| 1755 | int nand_prog_page_begin_op(struct nand_chip *chip, unsigned int page, |
| 1756 | unsigned int offset_in_page, const void *buf, |
| 1757 | unsigned int len) |
| 1758 | { |
| 1759 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1760 | |
| 1761 | if (len && !buf) |
| 1762 | return -EINVAL; |
| 1763 | |
| 1764 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1765 | return -EINVAL; |
| 1766 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1767 | if (chip->exec_op) |
| 1768 | return nand_exec_prog_page_op(chip, page, offset_in_page, buf, |
| 1769 | len, false); |
| 1770 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1771 | chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, page); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1772 | |
| 1773 | if (buf) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1774 | chip->legacy.write_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1775 | |
| 1776 | return 0; |
| 1777 | } |
| 1778 | EXPORT_SYMBOL_GPL(nand_prog_page_begin_op); |
| 1779 | |
| 1780 | /** |
| 1781 | * nand_prog_page_end_op - ends a PROG PAGE operation |
| 1782 | * @chip: The NAND chip |
| 1783 | * |
| 1784 | * This function issues the second half of a PROG PAGE operation. |
| 1785 | * This function does not select/unselect the CS line. |
| 1786 | * |
| 1787 | * Returns 0 on success, a negative error code otherwise. |
| 1788 | */ |
| 1789 | int nand_prog_page_end_op(struct nand_chip *chip) |
| 1790 | { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1791 | int ret; |
| 1792 | u8 status; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1793 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1794 | if (chip->exec_op) { |
| 1795 | const struct nand_sdr_timings *sdr = |
| 1796 | nand_get_sdr_timings(&chip->data_interface); |
| 1797 | struct nand_op_instr instrs[] = { |
| 1798 | NAND_OP_CMD(NAND_CMD_PAGEPROG, |
| 1799 | PSEC_TO_NSEC(sdr->tWB_max)), |
| 1800 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tPROG_max), 0), |
| 1801 | }; |
| 1802 | struct nand_operation op = NAND_OPERATION(instrs); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1803 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1804 | ret = nand_exec_op(chip, &op); |
| 1805 | if (ret) |
| 1806 | return ret; |
| 1807 | |
| 1808 | ret = nand_status_op(chip, &status); |
| 1809 | if (ret) |
| 1810 | return ret; |
| 1811 | } else { |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1812 | chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1); |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1813 | ret = chip->legacy.waitfunc(chip); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1814 | if (ret < 0) |
| 1815 | return ret; |
| 1816 | |
| 1817 | status = ret; |
| 1818 | } |
| 1819 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1820 | if (status & NAND_STATUS_FAIL) |
| 1821 | return -EIO; |
| 1822 | |
| 1823 | return 0; |
| 1824 | } |
| 1825 | EXPORT_SYMBOL_GPL(nand_prog_page_end_op); |
| 1826 | |
| 1827 | /** |
| 1828 | * nand_prog_page_op - Do a full PROG PAGE operation |
| 1829 | * @chip: The NAND chip |
| 1830 | * @page: page to write |
| 1831 | * @offset_in_page: offset within the page |
| 1832 | * @buf: buffer containing the data to write to the page |
| 1833 | * @len: length of the buffer |
| 1834 | * |
| 1835 | * This function issues a full PROG PAGE operation. |
| 1836 | * This function does not select/unselect the CS line. |
| 1837 | * |
| 1838 | * Returns 0 on success, a negative error code otherwise. |
| 1839 | */ |
| 1840 | int nand_prog_page_op(struct nand_chip *chip, unsigned int page, |
| 1841 | unsigned int offset_in_page, const void *buf, |
| 1842 | unsigned int len) |
| 1843 | { |
| 1844 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1845 | int status; |
| 1846 | |
| 1847 | if (!len || !buf) |
| 1848 | return -EINVAL; |
| 1849 | |
| 1850 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1851 | return -EINVAL; |
| 1852 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1853 | if (chip->exec_op) { |
| 1854 | status = nand_exec_prog_page_op(chip, page, offset_in_page, buf, |
| 1855 | len, true); |
| 1856 | } else { |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1857 | chip->legacy.cmdfunc(chip, NAND_CMD_SEQIN, offset_in_page, |
| 1858 | page); |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1859 | chip->legacy.write_buf(chip, buf, len); |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1860 | chip->legacy.cmdfunc(chip, NAND_CMD_PAGEPROG, -1, -1); |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 1861 | status = chip->legacy.waitfunc(chip); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1862 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1863 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1864 | if (status & NAND_STATUS_FAIL) |
| 1865 | return -EIO; |
| 1866 | |
| 1867 | return 0; |
| 1868 | } |
| 1869 | EXPORT_SYMBOL_GPL(nand_prog_page_op); |
| 1870 | |
| 1871 | /** |
| 1872 | * nand_change_write_column_op - Do a CHANGE WRITE COLUMN operation |
| 1873 | * @chip: The NAND chip |
| 1874 | * @offset_in_page: offset within the page |
| 1875 | * @buf: buffer containing the data to send to the NAND |
| 1876 | * @len: length of the buffer |
| 1877 | * @force_8bit: force 8-bit bus access |
| 1878 | * |
| 1879 | * This function issues a CHANGE WRITE COLUMN operation. |
| 1880 | * This function does not select/unselect the CS line. |
| 1881 | * |
| 1882 | * Returns 0 on success, a negative error code otherwise. |
| 1883 | */ |
| 1884 | int nand_change_write_column_op(struct nand_chip *chip, |
| 1885 | unsigned int offset_in_page, |
| 1886 | const void *buf, unsigned int len, |
| 1887 | bool force_8bit) |
| 1888 | { |
| 1889 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1890 | |
| 1891 | if (len && !buf) |
| 1892 | return -EINVAL; |
| 1893 | |
| 1894 | if (offset_in_page + len > mtd->writesize + mtd->oobsize) |
| 1895 | return -EINVAL; |
| 1896 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1897 | /* Small page NANDs do not support column change. */ |
| 1898 | if (mtd->writesize <= 512) |
| 1899 | return -ENOTSUPP; |
| 1900 | |
| 1901 | if (chip->exec_op) { |
| 1902 | const struct nand_sdr_timings *sdr = |
| 1903 | nand_get_sdr_timings(&chip->data_interface); |
| 1904 | u8 addrs[2]; |
| 1905 | struct nand_op_instr instrs[] = { |
| 1906 | NAND_OP_CMD(NAND_CMD_RNDIN, 0), |
| 1907 | NAND_OP_ADDR(2, addrs, PSEC_TO_NSEC(sdr->tCCS_min)), |
| 1908 | NAND_OP_DATA_OUT(len, buf, 0), |
| 1909 | }; |
| 1910 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1911 | int ret; |
| 1912 | |
| 1913 | ret = nand_fill_column_cycles(chip, addrs, offset_in_page); |
| 1914 | if (ret < 0) |
| 1915 | return ret; |
| 1916 | |
| 1917 | instrs[2].ctx.data.force_8bit = force_8bit; |
| 1918 | |
| 1919 | /* Drop the DATA_OUT instruction if len is set to 0. */ |
| 1920 | if (!len) |
| 1921 | op.ninstrs--; |
| 1922 | |
| 1923 | return nand_exec_op(chip, &op); |
| 1924 | } |
| 1925 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1926 | chip->legacy.cmdfunc(chip, NAND_CMD_RNDIN, offset_in_page, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1927 | if (len) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1928 | chip->legacy.write_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1929 | |
| 1930 | return 0; |
| 1931 | } |
| 1932 | EXPORT_SYMBOL_GPL(nand_change_write_column_op); |
| 1933 | |
| 1934 | /** |
| 1935 | * nand_readid_op - Do a READID operation |
| 1936 | * @chip: The NAND chip |
| 1937 | * @addr: address cycle to pass after the READID command |
| 1938 | * @buf: buffer used to store the ID |
| 1939 | * @len: length of the buffer |
| 1940 | * |
| 1941 | * This function sends a READID command and reads back the ID returned by the |
| 1942 | * NAND. |
| 1943 | * This function does not select/unselect the CS line. |
| 1944 | * |
| 1945 | * Returns 0 on success, a negative error code otherwise. |
| 1946 | */ |
| 1947 | int nand_readid_op(struct nand_chip *chip, u8 addr, void *buf, |
| 1948 | unsigned int len) |
| 1949 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1950 | unsigned int i; |
| 1951 | u8 *id = buf; |
| 1952 | |
| 1953 | if (len && !buf) |
| 1954 | return -EINVAL; |
| 1955 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1956 | if (chip->exec_op) { |
| 1957 | const struct nand_sdr_timings *sdr = |
| 1958 | nand_get_sdr_timings(&chip->data_interface); |
| 1959 | struct nand_op_instr instrs[] = { |
| 1960 | NAND_OP_CMD(NAND_CMD_READID, 0), |
| 1961 | NAND_OP_ADDR(1, &addr, PSEC_TO_NSEC(sdr->tADL_min)), |
| 1962 | NAND_OP_8BIT_DATA_IN(len, buf, 0), |
| 1963 | }; |
| 1964 | struct nand_operation op = NAND_OPERATION(instrs); |
| 1965 | |
| 1966 | /* Drop the DATA_IN instruction if len is set to 0. */ |
| 1967 | if (!len) |
| 1968 | op.ninstrs--; |
| 1969 | |
| 1970 | return nand_exec_op(chip, &op); |
| 1971 | } |
| 1972 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 1973 | chip->legacy.cmdfunc(chip, NAND_CMD_READID, addr, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1974 | |
| 1975 | for (i = 0; i < len; i++) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 1976 | id[i] = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1977 | |
| 1978 | return 0; |
| 1979 | } |
| 1980 | EXPORT_SYMBOL_GPL(nand_readid_op); |
| 1981 | |
| 1982 | /** |
| 1983 | * nand_status_op - Do a STATUS operation |
| 1984 | * @chip: The NAND chip |
| 1985 | * @status: out variable to store the NAND status |
| 1986 | * |
| 1987 | * This function sends a STATUS command and reads back the status returned by |
| 1988 | * the NAND. |
| 1989 | * This function does not select/unselect the CS line. |
| 1990 | * |
| 1991 | * Returns 0 on success, a negative error code otherwise. |
| 1992 | */ |
| 1993 | int nand_status_op(struct nand_chip *chip, u8 *status) |
| 1994 | { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 1995 | if (chip->exec_op) { |
| 1996 | const struct nand_sdr_timings *sdr = |
| 1997 | nand_get_sdr_timings(&chip->data_interface); |
| 1998 | struct nand_op_instr instrs[] = { |
| 1999 | NAND_OP_CMD(NAND_CMD_STATUS, |
| 2000 | PSEC_TO_NSEC(sdr->tADL_min)), |
| 2001 | NAND_OP_8BIT_DATA_IN(1, status, 0), |
| 2002 | }; |
| 2003 | struct nand_operation op = NAND_OPERATION(instrs); |
| 2004 | |
| 2005 | if (!status) |
| 2006 | op.ninstrs--; |
| 2007 | |
| 2008 | return nand_exec_op(chip, &op); |
| 2009 | } |
| 2010 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 2011 | chip->legacy.cmdfunc(chip, NAND_CMD_STATUS, -1, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2012 | if (status) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 2013 | *status = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2014 | |
| 2015 | return 0; |
| 2016 | } |
| 2017 | EXPORT_SYMBOL_GPL(nand_status_op); |
| 2018 | |
| 2019 | /** |
| 2020 | * nand_exit_status_op - Exit a STATUS operation |
| 2021 | * @chip: The NAND chip |
| 2022 | * |
| 2023 | * This function sends a READ0 command to cancel the effect of the STATUS |
| 2024 | * command to avoid reading only the status until a new read command is sent. |
| 2025 | * |
| 2026 | * This function does not select/unselect the CS line. |
| 2027 | * |
| 2028 | * Returns 0 on success, a negative error code otherwise. |
| 2029 | */ |
| 2030 | int nand_exit_status_op(struct nand_chip *chip) |
| 2031 | { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2032 | if (chip->exec_op) { |
| 2033 | struct nand_op_instr instrs[] = { |
| 2034 | NAND_OP_CMD(NAND_CMD_READ0, 0), |
| 2035 | }; |
| 2036 | struct nand_operation op = NAND_OPERATION(instrs); |
| 2037 | |
| 2038 | return nand_exec_op(chip, &op); |
| 2039 | } |
| 2040 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 2041 | chip->legacy.cmdfunc(chip, NAND_CMD_READ0, -1, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2042 | |
| 2043 | return 0; |
| 2044 | } |
| 2045 | EXPORT_SYMBOL_GPL(nand_exit_status_op); |
| 2046 | |
| 2047 | /** |
| 2048 | * nand_erase_op - Do an erase operation |
| 2049 | * @chip: The NAND chip |
| 2050 | * @eraseblock: block to erase |
| 2051 | * |
| 2052 | * This function sends an ERASE command and waits for the NAND to be ready |
| 2053 | * before returning. |
| 2054 | * This function does not select/unselect the CS line. |
| 2055 | * |
| 2056 | * Returns 0 on success, a negative error code otherwise. |
| 2057 | */ |
| 2058 | int nand_erase_op(struct nand_chip *chip, unsigned int eraseblock) |
| 2059 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2060 | unsigned int page = eraseblock << |
| 2061 | (chip->phys_erase_shift - chip->page_shift); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2062 | int ret; |
| 2063 | u8 status; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2064 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2065 | if (chip->exec_op) { |
| 2066 | const struct nand_sdr_timings *sdr = |
| 2067 | nand_get_sdr_timings(&chip->data_interface); |
| 2068 | u8 addrs[3] = { page, page >> 8, page >> 16 }; |
| 2069 | struct nand_op_instr instrs[] = { |
| 2070 | NAND_OP_CMD(NAND_CMD_ERASE1, 0), |
| 2071 | NAND_OP_ADDR(2, addrs, 0), |
| 2072 | NAND_OP_CMD(NAND_CMD_ERASE2, |
| 2073 | PSEC_TO_MSEC(sdr->tWB_max)), |
| 2074 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tBERS_max), 0), |
| 2075 | }; |
| 2076 | struct nand_operation op = NAND_OPERATION(instrs); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2077 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2078 | if (chip->options & NAND_ROW_ADDR_3) |
| 2079 | instrs[1].ctx.addr.naddrs++; |
| 2080 | |
| 2081 | ret = nand_exec_op(chip, &op); |
| 2082 | if (ret) |
| 2083 | return ret; |
| 2084 | |
| 2085 | ret = nand_status_op(chip, &status); |
| 2086 | if (ret) |
| 2087 | return ret; |
| 2088 | } else { |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 2089 | chip->legacy.cmdfunc(chip, NAND_CMD_ERASE1, -1, page); |
| 2090 | chip->legacy.cmdfunc(chip, NAND_CMD_ERASE2, -1, -1); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2091 | |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 2092 | ret = chip->legacy.waitfunc(chip); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2093 | if (ret < 0) |
| 2094 | return ret; |
| 2095 | |
| 2096 | status = ret; |
| 2097 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2098 | |
| 2099 | if (status & NAND_STATUS_FAIL) |
| 2100 | return -EIO; |
| 2101 | |
| 2102 | return 0; |
| 2103 | } |
| 2104 | EXPORT_SYMBOL_GPL(nand_erase_op); |
| 2105 | |
| 2106 | /** |
| 2107 | * nand_set_features_op - Do a SET FEATURES operation |
| 2108 | * @chip: The NAND chip |
| 2109 | * @feature: feature id |
| 2110 | * @data: 4 bytes of data |
| 2111 | * |
| 2112 | * This function sends a SET FEATURES command and waits for the NAND to be |
| 2113 | * ready before returning. |
| 2114 | * This function does not select/unselect the CS line. |
| 2115 | * |
| 2116 | * Returns 0 on success, a negative error code otherwise. |
| 2117 | */ |
| 2118 | static int nand_set_features_op(struct nand_chip *chip, u8 feature, |
| 2119 | const void *data) |
| 2120 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2121 | const u8 *params = data; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2122 | int i, ret; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2123 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2124 | if (chip->exec_op) { |
| 2125 | const struct nand_sdr_timings *sdr = |
| 2126 | nand_get_sdr_timings(&chip->data_interface); |
| 2127 | struct nand_op_instr instrs[] = { |
| 2128 | NAND_OP_CMD(NAND_CMD_SET_FEATURES, 0), |
| 2129 | NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tADL_min)), |
| 2130 | NAND_OP_8BIT_DATA_OUT(ONFI_SUBFEATURE_PARAM_LEN, data, |
| 2131 | PSEC_TO_NSEC(sdr->tWB_max)), |
| 2132 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), 0), |
| 2133 | }; |
| 2134 | struct nand_operation op = NAND_OPERATION(instrs); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2135 | |
Boris Brezillon | 782d196 | 2018-05-11 14:44:07 +0200 | [diff] [blame] | 2136 | return nand_exec_op(chip, &op); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2137 | } |
| 2138 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 2139 | chip->legacy.cmdfunc(chip, NAND_CMD_SET_FEATURES, feature, -1); |
Boris Brezillon | 782d196 | 2018-05-11 14:44:07 +0200 | [diff] [blame] | 2140 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 2141 | chip->legacy.write_byte(chip, params[i]); |
Boris Brezillon | 782d196 | 2018-05-11 14:44:07 +0200 | [diff] [blame] | 2142 | |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 2143 | ret = chip->legacy.waitfunc(chip); |
Boris Brezillon | 782d196 | 2018-05-11 14:44:07 +0200 | [diff] [blame] | 2144 | if (ret < 0) |
| 2145 | return ret; |
| 2146 | |
| 2147 | if (ret & NAND_STATUS_FAIL) |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2148 | return -EIO; |
| 2149 | |
| 2150 | return 0; |
| 2151 | } |
| 2152 | |
| 2153 | /** |
| 2154 | * nand_get_features_op - Do a GET FEATURES operation |
| 2155 | * @chip: The NAND chip |
| 2156 | * @feature: feature id |
| 2157 | * @data: 4 bytes of data |
| 2158 | * |
| 2159 | * This function sends a GET FEATURES command and waits for the NAND to be |
| 2160 | * ready before returning. |
| 2161 | * This function does not select/unselect the CS line. |
| 2162 | * |
| 2163 | * Returns 0 on success, a negative error code otherwise. |
| 2164 | */ |
| 2165 | static int nand_get_features_op(struct nand_chip *chip, u8 feature, |
| 2166 | void *data) |
| 2167 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2168 | u8 *params = data; |
| 2169 | int i; |
| 2170 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2171 | if (chip->exec_op) { |
| 2172 | const struct nand_sdr_timings *sdr = |
| 2173 | nand_get_sdr_timings(&chip->data_interface); |
| 2174 | struct nand_op_instr instrs[] = { |
| 2175 | NAND_OP_CMD(NAND_CMD_GET_FEATURES, 0), |
| 2176 | NAND_OP_ADDR(1, &feature, PSEC_TO_NSEC(sdr->tWB_max)), |
| 2177 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tFEAT_max), |
| 2178 | PSEC_TO_NSEC(sdr->tRR_min)), |
| 2179 | NAND_OP_8BIT_DATA_IN(ONFI_SUBFEATURE_PARAM_LEN, |
| 2180 | data, 0), |
| 2181 | }; |
| 2182 | struct nand_operation op = NAND_OPERATION(instrs); |
| 2183 | |
| 2184 | return nand_exec_op(chip, &op); |
| 2185 | } |
| 2186 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 2187 | chip->legacy.cmdfunc(chip, NAND_CMD_GET_FEATURES, feature, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2188 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 2189 | params[i] = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2190 | |
| 2191 | return 0; |
| 2192 | } |
| 2193 | |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 2194 | static int nand_wait_rdy_op(struct nand_chip *chip, unsigned int timeout_ms, |
| 2195 | unsigned int delay_ns) |
| 2196 | { |
| 2197 | if (chip->exec_op) { |
| 2198 | struct nand_op_instr instrs[] = { |
| 2199 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(timeout_ms), |
| 2200 | PSEC_TO_NSEC(delay_ns)), |
| 2201 | }; |
| 2202 | struct nand_operation op = NAND_OPERATION(instrs); |
| 2203 | |
| 2204 | return nand_exec_op(chip, &op); |
| 2205 | } |
| 2206 | |
| 2207 | /* Apply delay or wait for ready/busy pin */ |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 2208 | if (!chip->legacy.dev_ready) |
Boris Brezillon | 3cece3a | 2018-09-07 00:38:41 +0200 | [diff] [blame] | 2209 | udelay(chip->legacy.chip_delay); |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 2210 | else |
Boris Brezillon | 2b356ab | 2018-09-06 14:05:16 +0200 | [diff] [blame] | 2211 | nand_wait_ready(chip); |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 2212 | |
| 2213 | return 0; |
| 2214 | } |
| 2215 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2216 | /** |
| 2217 | * nand_reset_op - Do a reset operation |
| 2218 | * @chip: The NAND chip |
| 2219 | * |
| 2220 | * This function sends a RESET command and waits for the NAND to be ready |
| 2221 | * before returning. |
| 2222 | * This function does not select/unselect the CS line. |
| 2223 | * |
| 2224 | * Returns 0 on success, a negative error code otherwise. |
| 2225 | */ |
| 2226 | int nand_reset_op(struct nand_chip *chip) |
| 2227 | { |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2228 | if (chip->exec_op) { |
| 2229 | const struct nand_sdr_timings *sdr = |
| 2230 | nand_get_sdr_timings(&chip->data_interface); |
| 2231 | struct nand_op_instr instrs[] = { |
| 2232 | NAND_OP_CMD(NAND_CMD_RESET, PSEC_TO_NSEC(sdr->tWB_max)), |
| 2233 | NAND_OP_WAIT_RDY(PSEC_TO_MSEC(sdr->tRST_max), 0), |
| 2234 | }; |
| 2235 | struct nand_operation op = NAND_OPERATION(instrs); |
| 2236 | |
| 2237 | return nand_exec_op(chip, &op); |
| 2238 | } |
| 2239 | |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 2240 | chip->legacy.cmdfunc(chip, NAND_CMD_RESET, -1, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2241 | |
| 2242 | return 0; |
| 2243 | } |
| 2244 | EXPORT_SYMBOL_GPL(nand_reset_op); |
| 2245 | |
| 2246 | /** |
| 2247 | * nand_read_data_op - Read data from the NAND |
| 2248 | * @chip: The NAND chip |
| 2249 | * @buf: buffer used to store the data |
| 2250 | * @len: length of the buffer |
| 2251 | * @force_8bit: force 8-bit bus access |
| 2252 | * |
| 2253 | * This function does a raw data read on the bus. Usually used after launching |
| 2254 | * another NAND operation like nand_read_page_op(). |
| 2255 | * This function does not select/unselect the CS line. |
| 2256 | * |
| 2257 | * Returns 0 on success, a negative error code otherwise. |
| 2258 | */ |
| 2259 | int nand_read_data_op(struct nand_chip *chip, void *buf, unsigned int len, |
| 2260 | bool force_8bit) |
| 2261 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2262 | if (!len || !buf) |
| 2263 | return -EINVAL; |
| 2264 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2265 | if (chip->exec_op) { |
| 2266 | struct nand_op_instr instrs[] = { |
| 2267 | NAND_OP_DATA_IN(len, buf, 0), |
| 2268 | }; |
| 2269 | struct nand_operation op = NAND_OPERATION(instrs); |
| 2270 | |
| 2271 | instrs[0].ctx.data.force_8bit = force_8bit; |
| 2272 | |
| 2273 | return nand_exec_op(chip, &op); |
| 2274 | } |
| 2275 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2276 | if (force_8bit) { |
| 2277 | u8 *p = buf; |
| 2278 | unsigned int i; |
| 2279 | |
| 2280 | for (i = 0; i < len; i++) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 2281 | p[i] = chip->legacy.read_byte(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2282 | } else { |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 2283 | chip->legacy.read_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2284 | } |
| 2285 | |
| 2286 | return 0; |
| 2287 | } |
| 2288 | EXPORT_SYMBOL_GPL(nand_read_data_op); |
| 2289 | |
| 2290 | /** |
| 2291 | * nand_write_data_op - Write data from the NAND |
| 2292 | * @chip: The NAND chip |
| 2293 | * @buf: buffer containing the data to send on the bus |
| 2294 | * @len: length of the buffer |
| 2295 | * @force_8bit: force 8-bit bus access |
| 2296 | * |
| 2297 | * This function does a raw data write on the bus. Usually used after launching |
| 2298 | * another NAND operation like nand_write_page_begin_op(). |
| 2299 | * This function does not select/unselect the CS line. |
| 2300 | * |
| 2301 | * Returns 0 on success, a negative error code otherwise. |
| 2302 | */ |
| 2303 | int nand_write_data_op(struct nand_chip *chip, const void *buf, |
| 2304 | unsigned int len, bool force_8bit) |
| 2305 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2306 | if (!len || !buf) |
| 2307 | return -EINVAL; |
| 2308 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2309 | if (chip->exec_op) { |
| 2310 | struct nand_op_instr instrs[] = { |
| 2311 | NAND_OP_DATA_OUT(len, buf, 0), |
| 2312 | }; |
| 2313 | struct nand_operation op = NAND_OPERATION(instrs); |
| 2314 | |
| 2315 | instrs[0].ctx.data.force_8bit = force_8bit; |
| 2316 | |
| 2317 | return nand_exec_op(chip, &op); |
| 2318 | } |
| 2319 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2320 | if (force_8bit) { |
| 2321 | const u8 *p = buf; |
| 2322 | unsigned int i; |
| 2323 | |
| 2324 | for (i = 0; i < len; i++) |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 2325 | chip->legacy.write_byte(chip, p[i]); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2326 | } else { |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 2327 | chip->legacy.write_buf(chip, buf, len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | return 0; |
| 2331 | } |
| 2332 | EXPORT_SYMBOL_GPL(nand_write_data_op); |
| 2333 | |
| 2334 | /** |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2335 | * struct nand_op_parser_ctx - Context used by the parser |
| 2336 | * @instrs: array of all the instructions that must be addressed |
| 2337 | * @ninstrs: length of the @instrs array |
| 2338 | * @subop: Sub-operation to be passed to the NAND controller |
| 2339 | * |
| 2340 | * This structure is used by the core to split NAND operations into |
| 2341 | * sub-operations that can be handled by the NAND controller. |
| 2342 | */ |
| 2343 | struct nand_op_parser_ctx { |
| 2344 | const struct nand_op_instr *instrs; |
| 2345 | unsigned int ninstrs; |
| 2346 | struct nand_subop subop; |
| 2347 | }; |
| 2348 | |
| 2349 | /** |
| 2350 | * nand_op_parser_must_split_instr - Checks if an instruction must be split |
| 2351 | * @pat: the parser pattern element that matches @instr |
| 2352 | * @instr: pointer to the instruction to check |
| 2353 | * @start_offset: this is an in/out parameter. If @instr has already been |
| 2354 | * split, then @start_offset is the offset from which to start |
| 2355 | * (either an address cycle or an offset in the data buffer). |
| 2356 | * Conversely, if the function returns true (ie. instr must be |
| 2357 | * split), this parameter is updated to point to the first |
| 2358 | * data/address cycle that has not been taken care of. |
| 2359 | * |
| 2360 | * Some NAND controllers are limited and cannot send X address cycles with a |
| 2361 | * unique operation, or cannot read/write more than Y bytes at the same time. |
| 2362 | * In this case, split the instruction that does not fit in a single |
| 2363 | * controller-operation into two or more chunks. |
| 2364 | * |
| 2365 | * Returns true if the instruction must be split, false otherwise. |
| 2366 | * The @start_offset parameter is also updated to the offset at which the next |
| 2367 | * bundle of instruction must start (if an address or a data instruction). |
| 2368 | */ |
| 2369 | static bool |
| 2370 | nand_op_parser_must_split_instr(const struct nand_op_parser_pattern_elem *pat, |
| 2371 | const struct nand_op_instr *instr, |
| 2372 | unsigned int *start_offset) |
| 2373 | { |
| 2374 | switch (pat->type) { |
| 2375 | case NAND_OP_ADDR_INSTR: |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 2376 | if (!pat->ctx.addr.maxcycles) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2377 | break; |
| 2378 | |
| 2379 | if (instr->ctx.addr.naddrs - *start_offset > |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 2380 | pat->ctx.addr.maxcycles) { |
| 2381 | *start_offset += pat->ctx.addr.maxcycles; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2382 | return true; |
| 2383 | } |
| 2384 | break; |
| 2385 | |
| 2386 | case NAND_OP_DATA_IN_INSTR: |
| 2387 | case NAND_OP_DATA_OUT_INSTR: |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 2388 | if (!pat->ctx.data.maxlen) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2389 | break; |
| 2390 | |
Miquel Raynal | c1a72e2 | 2018-01-19 19:11:27 +0100 | [diff] [blame] | 2391 | if (instr->ctx.data.len - *start_offset > |
| 2392 | pat->ctx.data.maxlen) { |
| 2393 | *start_offset += pat->ctx.data.maxlen; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2394 | return true; |
| 2395 | } |
| 2396 | break; |
| 2397 | |
| 2398 | default: |
| 2399 | break; |
| 2400 | } |
| 2401 | |
| 2402 | return false; |
| 2403 | } |
| 2404 | |
| 2405 | /** |
| 2406 | * nand_op_parser_match_pat - Checks if a pattern matches the instructions |
| 2407 | * remaining in the parser context |
| 2408 | * @pat: the pattern to test |
| 2409 | * @ctx: the parser context structure to match with the pattern @pat |
| 2410 | * |
| 2411 | * Check if @pat matches the set or a sub-set of instructions remaining in @ctx. |
| 2412 | * Returns true if this is the case, false ortherwise. When true is returned, |
| 2413 | * @ctx->subop is updated with the set of instructions to be passed to the |
| 2414 | * controller driver. |
| 2415 | */ |
| 2416 | static bool |
| 2417 | nand_op_parser_match_pat(const struct nand_op_parser_pattern *pat, |
| 2418 | struct nand_op_parser_ctx *ctx) |
| 2419 | { |
| 2420 | unsigned int instr_offset = ctx->subop.first_instr_start_off; |
| 2421 | const struct nand_op_instr *end = ctx->instrs + ctx->ninstrs; |
| 2422 | const struct nand_op_instr *instr = ctx->subop.instrs; |
| 2423 | unsigned int i, ninstrs; |
| 2424 | |
| 2425 | for (i = 0, ninstrs = 0; i < pat->nelems && instr < end; i++) { |
| 2426 | /* |
| 2427 | * The pattern instruction does not match the operation |
| 2428 | * instruction. If the instruction is marked optional in the |
| 2429 | * pattern definition, we skip the pattern element and continue |
| 2430 | * to the next one. If the element is mandatory, there's no |
| 2431 | * match and we can return false directly. |
| 2432 | */ |
| 2433 | if (instr->type != pat->elems[i].type) { |
| 2434 | if (!pat->elems[i].optional) |
| 2435 | return false; |
| 2436 | |
| 2437 | continue; |
| 2438 | } |
| 2439 | |
| 2440 | /* |
| 2441 | * Now check the pattern element constraints. If the pattern is |
| 2442 | * not able to handle the whole instruction in a single step, |
| 2443 | * we have to split it. |
| 2444 | * The last_instr_end_off value comes back updated to point to |
| 2445 | * the position where we have to split the instruction (the |
| 2446 | * start of the next subop chunk). |
| 2447 | */ |
| 2448 | if (nand_op_parser_must_split_instr(&pat->elems[i], instr, |
| 2449 | &instr_offset)) { |
| 2450 | ninstrs++; |
| 2451 | i++; |
| 2452 | break; |
| 2453 | } |
| 2454 | |
| 2455 | instr++; |
| 2456 | ninstrs++; |
| 2457 | instr_offset = 0; |
| 2458 | } |
| 2459 | |
| 2460 | /* |
| 2461 | * This can happen if all instructions of a pattern are optional. |
| 2462 | * Still, if there's not at least one instruction handled by this |
| 2463 | * pattern, this is not a match, and we should try the next one (if |
| 2464 | * any). |
| 2465 | */ |
| 2466 | if (!ninstrs) |
| 2467 | return false; |
| 2468 | |
| 2469 | /* |
| 2470 | * We had a match on the pattern head, but the pattern may be longer |
| 2471 | * than the instructions we're asked to execute. We need to make sure |
| 2472 | * there's no mandatory elements in the pattern tail. |
| 2473 | */ |
| 2474 | for (; i < pat->nelems; i++) { |
| 2475 | if (!pat->elems[i].optional) |
| 2476 | return false; |
| 2477 | } |
| 2478 | |
| 2479 | /* |
| 2480 | * We have a match: update the subop structure accordingly and return |
| 2481 | * true. |
| 2482 | */ |
| 2483 | ctx->subop.ninstrs = ninstrs; |
| 2484 | ctx->subop.last_instr_end_off = instr_offset; |
| 2485 | |
| 2486 | return true; |
| 2487 | } |
| 2488 | |
| 2489 | #if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG) |
| 2490 | static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx) |
| 2491 | { |
| 2492 | const struct nand_op_instr *instr; |
| 2493 | char *prefix = " "; |
| 2494 | unsigned int i; |
| 2495 | |
| 2496 | pr_debug("executing subop:\n"); |
| 2497 | |
| 2498 | for (i = 0; i < ctx->ninstrs; i++) { |
| 2499 | instr = &ctx->instrs[i]; |
| 2500 | |
| 2501 | if (instr == &ctx->subop.instrs[0]) |
| 2502 | prefix = " ->"; |
| 2503 | |
| 2504 | switch (instr->type) { |
| 2505 | case NAND_OP_CMD_INSTR: |
| 2506 | pr_debug("%sCMD [0x%02x]\n", prefix, |
| 2507 | instr->ctx.cmd.opcode); |
| 2508 | break; |
| 2509 | case NAND_OP_ADDR_INSTR: |
| 2510 | pr_debug("%sADDR [%d cyc: %*ph]\n", prefix, |
| 2511 | instr->ctx.addr.naddrs, |
| 2512 | instr->ctx.addr.naddrs < 64 ? |
| 2513 | instr->ctx.addr.naddrs : 64, |
| 2514 | instr->ctx.addr.addrs); |
| 2515 | break; |
| 2516 | case NAND_OP_DATA_IN_INSTR: |
| 2517 | pr_debug("%sDATA_IN [%d B%s]\n", prefix, |
| 2518 | instr->ctx.data.len, |
| 2519 | instr->ctx.data.force_8bit ? |
| 2520 | ", force 8-bit" : ""); |
| 2521 | break; |
| 2522 | case NAND_OP_DATA_OUT_INSTR: |
| 2523 | pr_debug("%sDATA_OUT [%d B%s]\n", prefix, |
| 2524 | instr->ctx.data.len, |
| 2525 | instr->ctx.data.force_8bit ? |
| 2526 | ", force 8-bit" : ""); |
| 2527 | break; |
| 2528 | case NAND_OP_WAITRDY_INSTR: |
| 2529 | pr_debug("%sWAITRDY [max %d ms]\n", prefix, |
| 2530 | instr->ctx.waitrdy.timeout_ms); |
| 2531 | break; |
| 2532 | } |
| 2533 | |
| 2534 | if (instr == &ctx->subop.instrs[ctx->subop.ninstrs - 1]) |
| 2535 | prefix = " "; |
| 2536 | } |
| 2537 | } |
| 2538 | #else |
| 2539 | static void nand_op_parser_trace(const struct nand_op_parser_ctx *ctx) |
| 2540 | { |
| 2541 | /* NOP */ |
| 2542 | } |
| 2543 | #endif |
| 2544 | |
| 2545 | /** |
| 2546 | * nand_op_parser_exec_op - exec_op parser |
| 2547 | * @chip: the NAND chip |
| 2548 | * @parser: patterns description provided by the controller driver |
| 2549 | * @op: the NAND operation to address |
| 2550 | * @check_only: when true, the function only checks if @op can be handled but |
| 2551 | * does not execute the operation |
| 2552 | * |
| 2553 | * Helper function designed to ease integration of NAND controller drivers that |
| 2554 | * only support a limited set of instruction sequences. The supported sequences |
| 2555 | * are described in @parser, and the framework takes care of splitting @op into |
| 2556 | * multiple sub-operations (if required) and pass them back to the ->exec() |
| 2557 | * callback of the matching pattern if @check_only is set to false. |
| 2558 | * |
| 2559 | * NAND controller drivers should call this function from their own ->exec_op() |
| 2560 | * implementation. |
| 2561 | * |
| 2562 | * Returns 0 on success, a negative error code otherwise. A failure can be |
| 2563 | * caused by an unsupported operation (none of the supported patterns is able |
| 2564 | * to handle the requested operation), or an error returned by one of the |
| 2565 | * matching pattern->exec() hook. |
| 2566 | */ |
| 2567 | int nand_op_parser_exec_op(struct nand_chip *chip, |
| 2568 | const struct nand_op_parser *parser, |
| 2569 | const struct nand_operation *op, bool check_only) |
| 2570 | { |
| 2571 | struct nand_op_parser_ctx ctx = { |
| 2572 | .subop.instrs = op->instrs, |
| 2573 | .instrs = op->instrs, |
| 2574 | .ninstrs = op->ninstrs, |
| 2575 | }; |
| 2576 | unsigned int i; |
| 2577 | |
| 2578 | while (ctx.subop.instrs < op->instrs + op->ninstrs) { |
| 2579 | int ret; |
| 2580 | |
| 2581 | for (i = 0; i < parser->npatterns; i++) { |
| 2582 | const struct nand_op_parser_pattern *pattern; |
| 2583 | |
| 2584 | pattern = &parser->patterns[i]; |
| 2585 | if (!nand_op_parser_match_pat(pattern, &ctx)) |
| 2586 | continue; |
| 2587 | |
| 2588 | nand_op_parser_trace(&ctx); |
| 2589 | |
| 2590 | if (check_only) |
| 2591 | break; |
| 2592 | |
| 2593 | ret = pattern->exec(chip, &ctx.subop); |
| 2594 | if (ret) |
| 2595 | return ret; |
| 2596 | |
| 2597 | break; |
| 2598 | } |
| 2599 | |
| 2600 | if (i == parser->npatterns) { |
| 2601 | pr_debug("->exec_op() parser: pattern not found!\n"); |
| 2602 | return -ENOTSUPP; |
| 2603 | } |
| 2604 | |
| 2605 | /* |
| 2606 | * Update the context structure by pointing to the start of the |
| 2607 | * next subop. |
| 2608 | */ |
| 2609 | ctx.subop.instrs = ctx.subop.instrs + ctx.subop.ninstrs; |
| 2610 | if (ctx.subop.last_instr_end_off) |
| 2611 | ctx.subop.instrs -= 1; |
| 2612 | |
| 2613 | ctx.subop.first_instr_start_off = ctx.subop.last_instr_end_off; |
| 2614 | } |
| 2615 | |
| 2616 | return 0; |
| 2617 | } |
| 2618 | EXPORT_SYMBOL_GPL(nand_op_parser_exec_op); |
| 2619 | |
| 2620 | static bool nand_instr_is_data(const struct nand_op_instr *instr) |
| 2621 | { |
| 2622 | return instr && (instr->type == NAND_OP_DATA_IN_INSTR || |
| 2623 | instr->type == NAND_OP_DATA_OUT_INSTR); |
| 2624 | } |
| 2625 | |
| 2626 | static bool nand_subop_instr_is_valid(const struct nand_subop *subop, |
| 2627 | unsigned int instr_idx) |
| 2628 | { |
| 2629 | return subop && instr_idx < subop->ninstrs; |
| 2630 | } |
| 2631 | |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2632 | static unsigned int nand_subop_get_start_off(const struct nand_subop *subop, |
| 2633 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2634 | { |
| 2635 | if (instr_idx) |
| 2636 | return 0; |
| 2637 | |
| 2638 | return subop->first_instr_start_off; |
| 2639 | } |
| 2640 | |
| 2641 | /** |
| 2642 | * nand_subop_get_addr_start_off - Get the start offset in an address array |
| 2643 | * @subop: The entire sub-operation |
| 2644 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2645 | * |
| 2646 | * During driver development, one could be tempted to directly use the |
| 2647 | * ->addr.addrs field of address instructions. This is wrong as address |
| 2648 | * instructions might be split. |
| 2649 | * |
| 2650 | * Given an address instruction, returns the offset of the first cycle to issue. |
| 2651 | */ |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2652 | unsigned int nand_subop_get_addr_start_off(const struct nand_subop *subop, |
| 2653 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2654 | { |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2655 | if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2656 | subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)) |
| 2657 | return 0; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2658 | |
| 2659 | return nand_subop_get_start_off(subop, instr_idx); |
| 2660 | } |
| 2661 | EXPORT_SYMBOL_GPL(nand_subop_get_addr_start_off); |
| 2662 | |
| 2663 | /** |
| 2664 | * nand_subop_get_num_addr_cyc - Get the remaining address cycles to assert |
| 2665 | * @subop: The entire sub-operation |
| 2666 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2667 | * |
| 2668 | * During driver development, one could be tempted to directly use the |
| 2669 | * ->addr->naddrs field of a data instruction. This is wrong as instructions |
| 2670 | * might be split. |
| 2671 | * |
| 2672 | * Given an address instruction, returns the number of address cycle to issue. |
| 2673 | */ |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2674 | unsigned int nand_subop_get_num_addr_cyc(const struct nand_subop *subop, |
| 2675 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2676 | { |
| 2677 | int start_off, end_off; |
| 2678 | |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2679 | if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2680 | subop->instrs[instr_idx].type != NAND_OP_ADDR_INSTR)) |
| 2681 | return 0; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2682 | |
| 2683 | start_off = nand_subop_get_addr_start_off(subop, instr_idx); |
| 2684 | |
| 2685 | if (instr_idx == subop->ninstrs - 1 && |
| 2686 | subop->last_instr_end_off) |
| 2687 | end_off = subop->last_instr_end_off; |
| 2688 | else |
| 2689 | end_off = subop->instrs[instr_idx].ctx.addr.naddrs; |
| 2690 | |
| 2691 | return end_off - start_off; |
| 2692 | } |
| 2693 | EXPORT_SYMBOL_GPL(nand_subop_get_num_addr_cyc); |
| 2694 | |
| 2695 | /** |
| 2696 | * nand_subop_get_data_start_off - Get the start offset in a data array |
| 2697 | * @subop: The entire sub-operation |
| 2698 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2699 | * |
| 2700 | * During driver development, one could be tempted to directly use the |
| 2701 | * ->data->buf.{in,out} field of data instructions. This is wrong as data |
| 2702 | * instructions might be split. |
| 2703 | * |
| 2704 | * Given a data instruction, returns the offset to start from. |
| 2705 | */ |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2706 | unsigned int nand_subop_get_data_start_off(const struct nand_subop *subop, |
| 2707 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2708 | { |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2709 | if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2710 | !nand_instr_is_data(&subop->instrs[instr_idx]))) |
| 2711 | return 0; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2712 | |
| 2713 | return nand_subop_get_start_off(subop, instr_idx); |
| 2714 | } |
| 2715 | EXPORT_SYMBOL_GPL(nand_subop_get_data_start_off); |
| 2716 | |
| 2717 | /** |
| 2718 | * nand_subop_get_data_len - Get the number of bytes to retrieve |
| 2719 | * @subop: The entire sub-operation |
| 2720 | * @instr_idx: Index of the instruction inside the sub-operation |
| 2721 | * |
| 2722 | * During driver development, one could be tempted to directly use the |
| 2723 | * ->data->len field of a data instruction. This is wrong as data instructions |
| 2724 | * might be split. |
| 2725 | * |
| 2726 | * Returns the length of the chunk of data to send/receive. |
| 2727 | */ |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2728 | unsigned int nand_subop_get_data_len(const struct nand_subop *subop, |
| 2729 | unsigned int instr_idx) |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2730 | { |
| 2731 | int start_off = 0, end_off; |
| 2732 | |
Miquel Raynal | 760c435 | 2018-07-19 00:09:12 +0200 | [diff] [blame] | 2733 | if (WARN_ON(!nand_subop_instr_is_valid(subop, instr_idx) || |
| 2734 | !nand_instr_is_data(&subop->instrs[instr_idx]))) |
| 2735 | return 0; |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 2736 | |
| 2737 | start_off = nand_subop_get_data_start_off(subop, instr_idx); |
| 2738 | |
| 2739 | if (instr_idx == subop->ninstrs - 1 && |
| 2740 | subop->last_instr_end_off) |
| 2741 | end_off = subop->last_instr_end_off; |
| 2742 | else |
| 2743 | end_off = subop->instrs[instr_idx].ctx.data.len; |
| 2744 | |
| 2745 | return end_off - start_off; |
| 2746 | } |
| 2747 | EXPORT_SYMBOL_GPL(nand_subop_get_data_len); |
| 2748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | /** |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2750 | * nand_reset - Reset and initialize a NAND device |
| 2751 | * @chip: The NAND chip |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2752 | * @chipnr: Internal die id |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2753 | * |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 2754 | * Save the timings data structure, then apply SDR timings mode 0 (see |
| 2755 | * nand_reset_data_interface for details), do the reset operation, and |
| 2756 | * apply back the previous timings. |
| 2757 | * |
| 2758 | * Returns 0 on success, a negative error code otherwise. |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2759 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2760 | int nand_reset(struct nand_chip *chip, int chipnr) |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2761 | { |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 2762 | struct nand_data_interface saved_data_intf = chip->data_interface; |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 2763 | int ret; |
| 2764 | |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 2765 | ret = nand_reset_data_interface(chip, chipnr); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 2766 | if (ret) |
| 2767 | return ret; |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2768 | |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2769 | /* |
| 2770 | * The CS line has to be released before we can apply the new NAND |
| 2771 | * interface settings, hence this weird ->select_chip() dance. |
| 2772 | */ |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 2773 | chip->select_chip(chip, chipnr); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2774 | ret = nand_reset_op(chip); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 2775 | chip->select_chip(chip, -1); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 2776 | if (ret) |
| 2777 | return ret; |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2778 | |
Miquel Raynal | 107b7d6 | 2018-03-19 14:47:25 +0100 | [diff] [blame] | 2779 | /* |
| 2780 | * A nand_reset_data_interface() put both the NAND chip and the NAND |
| 2781 | * controller in timings mode 0. If the default mode for this chip is |
| 2782 | * also 0, no need to proceed to the change again. Plus, at probe time, |
| 2783 | * nand_setup_data_interface() uses ->set/get_features() which would |
| 2784 | * fail anyway as the parameter page is not available yet. |
| 2785 | */ |
| 2786 | if (!chip->onfi_timing_mode_default) |
| 2787 | return 0; |
| 2788 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 2789 | chip->data_interface = saved_data_intf; |
Boris Brezillon | 104e442 | 2017-03-16 09:35:58 +0100 | [diff] [blame] | 2790 | ret = nand_setup_data_interface(chip, chipnr); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 2791 | if (ret) |
| 2792 | return ret; |
| 2793 | |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2794 | return 0; |
| 2795 | } |
Boris Brezillon | b9bb984 | 2017-10-05 18:53:19 +0200 | [diff] [blame] | 2796 | EXPORT_SYMBOL_GPL(nand_reset); |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 2797 | |
| 2798 | /** |
Boris Brezillon | 4524036 | 2018-09-07 00:38:40 +0200 | [diff] [blame] | 2799 | * nand_get_features - wrapper to perform a GET_FEATURE |
| 2800 | * @chip: NAND chip info structure |
| 2801 | * @addr: feature address |
| 2802 | * @subfeature_param: the subfeature parameters, a four bytes array |
| 2803 | * |
| 2804 | * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the |
| 2805 | * operation cannot be handled. |
| 2806 | */ |
| 2807 | int nand_get_features(struct nand_chip *chip, int addr, |
| 2808 | u8 *subfeature_param) |
| 2809 | { |
| 2810 | if (!nand_supports_get_features(chip, addr)) |
| 2811 | return -ENOTSUPP; |
| 2812 | |
| 2813 | if (chip->legacy.get_features) |
| 2814 | return chip->legacy.get_features(chip, addr, subfeature_param); |
| 2815 | |
| 2816 | return nand_get_features_op(chip, addr, subfeature_param); |
| 2817 | } |
| 2818 | EXPORT_SYMBOL_GPL(nand_get_features); |
| 2819 | |
| 2820 | /** |
| 2821 | * nand_set_features - wrapper to perform a SET_FEATURE |
| 2822 | * @chip: NAND chip info structure |
| 2823 | * @addr: feature address |
| 2824 | * @subfeature_param: the subfeature parameters, a four bytes array |
| 2825 | * |
| 2826 | * Returns 0 for success, a negative error otherwise. Returns -ENOTSUPP if the |
| 2827 | * operation cannot be handled. |
| 2828 | */ |
| 2829 | int nand_set_features(struct nand_chip *chip, int addr, |
| 2830 | u8 *subfeature_param) |
| 2831 | { |
| 2832 | if (!nand_supports_set_features(chip, addr)) |
| 2833 | return -ENOTSUPP; |
| 2834 | |
| 2835 | if (chip->legacy.set_features) |
| 2836 | return chip->legacy.set_features(chip, addr, subfeature_param); |
| 2837 | |
| 2838 | return nand_set_features_op(chip, addr, subfeature_param); |
| 2839 | } |
| 2840 | EXPORT_SYMBOL_GPL(nand_set_features); |
| 2841 | |
| 2842 | /** |
Boris BREZILLON | 730a43f | 2015-09-03 18:03:38 +0200 | [diff] [blame] | 2843 | * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data |
| 2844 | * @buf: buffer to test |
| 2845 | * @len: buffer length |
| 2846 | * @bitflips_threshold: maximum number of bitflips |
| 2847 | * |
| 2848 | * Check if a buffer contains only 0xff, which means the underlying region |
| 2849 | * has been erased and is ready to be programmed. |
| 2850 | * The bitflips_threshold specify the maximum number of bitflips before |
| 2851 | * considering the region is not erased. |
| 2852 | * Note: The logic of this function has been extracted from the memweight |
| 2853 | * implementation, except that nand_check_erased_buf function exit before |
| 2854 | * testing the whole buffer if the number of bitflips exceed the |
| 2855 | * bitflips_threshold value. |
| 2856 | * |
| 2857 | * Returns a positive number of bitflips less than or equal to |
| 2858 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 2859 | * threshold. |
| 2860 | */ |
| 2861 | static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold) |
| 2862 | { |
| 2863 | const unsigned char *bitmap = buf; |
| 2864 | int bitflips = 0; |
| 2865 | int weight; |
| 2866 | |
| 2867 | for (; len && ((uintptr_t)bitmap) % sizeof(long); |
| 2868 | len--, bitmap++) { |
| 2869 | weight = hweight8(*bitmap); |
| 2870 | bitflips += BITS_PER_BYTE - weight; |
| 2871 | if (unlikely(bitflips > bitflips_threshold)) |
| 2872 | return -EBADMSG; |
| 2873 | } |
| 2874 | |
| 2875 | for (; len >= sizeof(long); |
| 2876 | len -= sizeof(long), bitmap += sizeof(long)) { |
Pavel Machek | 086567f | 2017-04-21 12:51:07 +0200 | [diff] [blame] | 2877 | unsigned long d = *((unsigned long *)bitmap); |
| 2878 | if (d == ~0UL) |
| 2879 | continue; |
| 2880 | weight = hweight_long(d); |
Boris BREZILLON | 730a43f | 2015-09-03 18:03:38 +0200 | [diff] [blame] | 2881 | bitflips += BITS_PER_LONG - weight; |
| 2882 | if (unlikely(bitflips > bitflips_threshold)) |
| 2883 | return -EBADMSG; |
| 2884 | } |
| 2885 | |
| 2886 | for (; len > 0; len--, bitmap++) { |
| 2887 | weight = hweight8(*bitmap); |
| 2888 | bitflips += BITS_PER_BYTE - weight; |
| 2889 | if (unlikely(bitflips > bitflips_threshold)) |
| 2890 | return -EBADMSG; |
| 2891 | } |
| 2892 | |
| 2893 | return bitflips; |
| 2894 | } |
| 2895 | |
| 2896 | /** |
| 2897 | * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only |
| 2898 | * 0xff data |
| 2899 | * @data: data buffer to test |
| 2900 | * @datalen: data length |
| 2901 | * @ecc: ECC buffer |
| 2902 | * @ecclen: ECC length |
| 2903 | * @extraoob: extra OOB buffer |
| 2904 | * @extraooblen: extra OOB length |
| 2905 | * @bitflips_threshold: maximum number of bitflips |
| 2906 | * |
| 2907 | * Check if a data buffer and its associated ECC and OOB data contains only |
| 2908 | * 0xff pattern, which means the underlying region has been erased and is |
| 2909 | * ready to be programmed. |
| 2910 | * The bitflips_threshold specify the maximum number of bitflips before |
| 2911 | * considering the region as not erased. |
| 2912 | * |
| 2913 | * Note: |
| 2914 | * 1/ ECC algorithms are working on pre-defined block sizes which are usually |
| 2915 | * different from the NAND page size. When fixing bitflips, ECC engines will |
| 2916 | * report the number of errors per chunk, and the NAND core infrastructure |
| 2917 | * expect you to return the maximum number of bitflips for the whole page. |
| 2918 | * This is why you should always use this function on a single chunk and |
| 2919 | * not on the whole page. After checking each chunk you should update your |
| 2920 | * max_bitflips value accordingly. |
| 2921 | * 2/ When checking for bitflips in erased pages you should not only check |
| 2922 | * the payload data but also their associated ECC data, because a user might |
| 2923 | * have programmed almost all bits to 1 but a few. In this case, we |
| 2924 | * shouldn't consider the chunk as erased, and checking ECC bytes prevent |
| 2925 | * this case. |
| 2926 | * 3/ The extraoob argument is optional, and should be used if some of your OOB |
| 2927 | * data are protected by the ECC engine. |
| 2928 | * It could also be used if you support subpages and want to attach some |
| 2929 | * extra OOB data to an ECC chunk. |
| 2930 | * |
| 2931 | * Returns a positive number of bitflips less than or equal to |
| 2932 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 2933 | * threshold. In case of success, the passed buffers are filled with 0xff. |
| 2934 | */ |
| 2935 | int nand_check_erased_ecc_chunk(void *data, int datalen, |
| 2936 | void *ecc, int ecclen, |
| 2937 | void *extraoob, int extraooblen, |
| 2938 | int bitflips_threshold) |
| 2939 | { |
| 2940 | int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0; |
| 2941 | |
| 2942 | data_bitflips = nand_check_erased_buf(data, datalen, |
| 2943 | bitflips_threshold); |
| 2944 | if (data_bitflips < 0) |
| 2945 | return data_bitflips; |
| 2946 | |
| 2947 | bitflips_threshold -= data_bitflips; |
| 2948 | |
| 2949 | ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold); |
| 2950 | if (ecc_bitflips < 0) |
| 2951 | return ecc_bitflips; |
| 2952 | |
| 2953 | bitflips_threshold -= ecc_bitflips; |
| 2954 | |
| 2955 | extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen, |
| 2956 | bitflips_threshold); |
| 2957 | if (extraoob_bitflips < 0) |
| 2958 | return extraoob_bitflips; |
| 2959 | |
| 2960 | if (data_bitflips) |
| 2961 | memset(data, 0xff, datalen); |
| 2962 | |
| 2963 | if (ecc_bitflips) |
| 2964 | memset(ecc, 0xff, ecclen); |
| 2965 | |
| 2966 | if (extraoob_bitflips) |
| 2967 | memset(extraoob, 0xff, extraooblen); |
| 2968 | |
| 2969 | return data_bitflips + ecc_bitflips + extraoob_bitflips; |
| 2970 | } |
| 2971 | EXPORT_SYMBOL(nand_check_erased_ecc_chunk); |
| 2972 | |
| 2973 | /** |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 2974 | * nand_read_page_raw_notsupp - dummy read raw page function |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 2975 | * @chip: nand chip info structure |
| 2976 | * @buf: buffer to store read data |
| 2977 | * @oob_required: caller requires OOB data read to chip->oob_poi |
| 2978 | * @page: page number to read |
| 2979 | * |
| 2980 | * Returns -ENOTSUPP unconditionally. |
| 2981 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2982 | int nand_read_page_raw_notsupp(struct nand_chip *chip, u8 *buf, |
| 2983 | int oob_required, int page) |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 2984 | { |
| 2985 | return -ENOTSUPP; |
| 2986 | } |
| 2987 | EXPORT_SYMBOL(nand_read_page_raw_notsupp); |
| 2988 | |
| 2989 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2990 | * nand_read_page_raw - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2991 | * @chip: nand chip info structure |
| 2992 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2993 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2994 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2995 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2996 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2997 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 2998 | int nand_read_page_raw(struct nand_chip *chip, uint8_t *buf, int oob_required, |
| 2999 | int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3000 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3001 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3002 | int ret; |
| 3003 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3004 | ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3005 | if (ret) |
| 3006 | return ret; |
| 3007 | |
| 3008 | if (oob_required) { |
| 3009 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 3010 | false); |
| 3011 | if (ret) |
| 3012 | return ret; |
| 3013 | } |
| 3014 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3015 | return 0; |
| 3016 | } |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 3017 | EXPORT_SYMBOL(nand_read_page_raw); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3018 | |
| 3019 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3020 | * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3021 | * @chip: nand chip info structure |
| 3022 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3023 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3024 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3025 | * |
| 3026 | * We need a special oob layout and handling even when OOB isn't used. |
| 3027 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3028 | static int nand_read_page_raw_syndrome(struct nand_chip *chip, uint8_t *buf, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3029 | int oob_required, int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3030 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3031 | struct mtd_info *mtd = nand_to_mtd(chip); |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3032 | int eccsize = chip->ecc.size; |
| 3033 | int eccbytes = chip->ecc.bytes; |
| 3034 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3035 | int steps, size, ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3036 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3037 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 3038 | if (ret) |
| 3039 | return ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3040 | |
| 3041 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3042 | ret = nand_read_data_op(chip, buf, eccsize, false); |
| 3043 | if (ret) |
| 3044 | return ret; |
| 3045 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3046 | buf += eccsize; |
| 3047 | |
| 3048 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3049 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 3050 | false); |
| 3051 | if (ret) |
| 3052 | return ret; |
| 3053 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3054 | oob += chip->ecc.prepad; |
| 3055 | } |
| 3056 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3057 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 3058 | if (ret) |
| 3059 | return ret; |
| 3060 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3061 | oob += eccbytes; |
| 3062 | |
| 3063 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3064 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 3065 | false); |
| 3066 | if (ret) |
| 3067 | return ret; |
| 3068 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3069 | oob += chip->ecc.postpad; |
| 3070 | } |
| 3071 | } |
| 3072 | |
| 3073 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3074 | if (size) { |
| 3075 | ret = nand_read_data_op(chip, oob, size, false); |
| 3076 | if (ret) |
| 3077 | return ret; |
| 3078 | } |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3079 | |
| 3080 | return 0; |
| 3081 | } |
| 3082 | |
| 3083 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3084 | * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3085 | * @chip: nand chip info structure |
| 3086 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3087 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3088 | * @page: page number to read |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 3089 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3090 | static int nand_read_page_swecc(struct nand_chip *chip, uint8_t *buf, |
| 3091 | int oob_required, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3093 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3094 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3095 | int eccbytes = chip->ecc.bytes; |
| 3096 | int eccsteps = chip->ecc.steps; |
| 3097 | uint8_t *p = buf; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3098 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
| 3099 | uint8_t *ecc_code = chip->ecc.code_buf; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3100 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3101 | |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3102 | chip->ecc.read_page_raw(chip, buf, 1, page); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3103 | |
| 3104 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 3105 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3106 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3107 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 3108 | chip->ecc.total); |
| 3109 | if (ret) |
| 3110 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3111 | |
| 3112 | eccsteps = chip->ecc.steps; |
| 3113 | p = buf; |
| 3114 | |
| 3115 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 3116 | int stat; |
| 3117 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 3118 | stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]); |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3119 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3120 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3121 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3122 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3123 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 3124 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3125 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3126 | return max_bitflips; |
Thomas Gleixner | 22c60f5 | 2005-04-04 19:56:32 +0100 | [diff] [blame] | 3127 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3129 | /** |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 3130 | * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3131 | * @chip: nand chip info structure |
| 3132 | * @data_offs: offset of requested data within the page |
| 3133 | * @readlen: data length |
| 3134 | * @bufpoi: buffer to store read data |
Huang Shijie | e004deb | 2014-01-03 11:01:40 +0800 | [diff] [blame] | 3135 | * @page: page number to read |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3136 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3137 | static int nand_read_subpage(struct nand_chip *chip, uint32_t data_offs, |
| 3138 | uint32_t readlen, uint8_t *bufpoi, int page) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3139 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3140 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3141 | int start_step, end_step, num_steps, ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3142 | uint8_t *p; |
| 3143 | int data_col_addr, i, gaps = 0; |
| 3144 | int datafrag_len, eccfrag_len, aligned_len, aligned_pos; |
| 3145 | int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3146 | int index, section = 0; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3147 | unsigned int max_bitflips = 0; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3148 | struct mtd_oob_region oobregion = { }; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3149 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3150 | /* Column address within the page aligned to ECC size (256bytes) */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3151 | start_step = data_offs / chip->ecc.size; |
| 3152 | end_step = (data_offs + readlen - 1) / chip->ecc.size; |
| 3153 | num_steps = end_step - start_step + 1; |
Ron | 4a4163ca | 2014-03-16 04:01:07 +1030 | [diff] [blame] | 3154 | index = start_step * chip->ecc.bytes; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3155 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3156 | /* Data size aligned to ECC ecc.size */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3157 | datafrag_len = num_steps * chip->ecc.size; |
| 3158 | eccfrag_len = num_steps * chip->ecc.bytes; |
| 3159 | |
| 3160 | data_col_addr = start_step * chip->ecc.size; |
| 3161 | /* If we read not a page aligned data */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3162 | p = bufpoi + data_col_addr; |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3163 | ret = nand_read_page_op(chip, page, data_col_addr, p, datafrag_len); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3164 | if (ret) |
| 3165 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3166 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3167 | /* Calculate ECC */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3168 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 3169 | chip->ecc.calculate(chip, p, &chip->ecc.calc_buf[i]); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3170 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3171 | /* |
| 3172 | * The performance is faster if we position offsets according to |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3173 | * 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] | 3174 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3175 | ret = mtd_ooblayout_find_eccregion(mtd, index, §ion, &oobregion); |
| 3176 | if (ret) |
| 3177 | return ret; |
| 3178 | |
| 3179 | if (oobregion.length < eccfrag_len) |
| 3180 | gaps = 1; |
| 3181 | |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3182 | if (gaps) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3183 | ret = nand_change_read_column_op(chip, mtd->writesize, |
| 3184 | chip->oob_poi, mtd->oobsize, |
| 3185 | false); |
| 3186 | if (ret) |
| 3187 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3188 | } else { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3189 | /* |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3190 | * Send the command to read the particular ECC bytes take care |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3191 | * about buswidth alignment in read_buf. |
| 3192 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3193 | aligned_pos = oobregion.offset & ~(busw - 1); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3194 | aligned_len = eccfrag_len; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3195 | if (oobregion.offset & (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3196 | aligned_len++; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3197 | if ((oobregion.offset + (num_steps * chip->ecc.bytes)) & |
| 3198 | (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3199 | aligned_len++; |
| 3200 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3201 | ret = nand_change_read_column_op(chip, |
| 3202 | mtd->writesize + aligned_pos, |
| 3203 | &chip->oob_poi[aligned_pos], |
| 3204 | aligned_len, false); |
| 3205 | if (ret) |
| 3206 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3207 | } |
| 3208 | |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3209 | ret = mtd_ooblayout_get_eccbytes(mtd, chip->ecc.code_buf, |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3210 | chip->oob_poi, index, eccfrag_len); |
| 3211 | if (ret) |
| 3212 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3213 | |
| 3214 | p = bufpoi + data_col_addr; |
| 3215 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { |
| 3216 | int stat; |
| 3217 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 3218 | stat = chip->ecc.correct(chip, p, &chip->ecc.code_buf[i], |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3219 | &chip->ecc.calc_buf[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 3220 | if (stat == -EBADMSG && |
| 3221 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 3222 | /* check for empty pages with bitflips */ |
| 3223 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3224 | &chip->ecc.code_buf[i], |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 3225 | chip->ecc.bytes, |
| 3226 | NULL, 0, |
| 3227 | chip->ecc.strength); |
| 3228 | } |
| 3229 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3230 | if (stat < 0) { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3231 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3232 | } else { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3233 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3234 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 3235 | } |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3236 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3237 | return max_bitflips; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3238 | } |
| 3239 | |
| 3240 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3241 | * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3242 | * @chip: nand chip info structure |
| 3243 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3244 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3245 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3246 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3247 | * Not for syndrome calculating ECC controllers which need a special oob layout. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3248 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3249 | static int nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf, |
| 3250 | int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3251 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3252 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3253 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3254 | int eccbytes = chip->ecc.bytes; |
| 3255 | int eccsteps = chip->ecc.steps; |
| 3256 | uint8_t *p = buf; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3257 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
| 3258 | uint8_t *ecc_code = chip->ecc.code_buf; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3259 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3260 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3261 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 3262 | if (ret) |
| 3263 | return ret; |
| 3264 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3265 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 3266 | chip->ecc.hwctl(chip, NAND_ECC_READ); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3267 | |
| 3268 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 3269 | if (ret) |
| 3270 | return ret; |
| 3271 | |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 3272 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3273 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3274 | |
| 3275 | ret = nand_read_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 3276 | if (ret) |
| 3277 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3278 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3279 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 3280 | chip->ecc.total); |
| 3281 | if (ret) |
| 3282 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3283 | |
| 3284 | eccsteps = chip->ecc.steps; |
| 3285 | p = buf; |
| 3286 | |
| 3287 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 3288 | int stat; |
| 3289 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 3290 | stat = chip->ecc.correct(chip, p, &ecc_code[i], &ecc_calc[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 3291 | if (stat == -EBADMSG && |
| 3292 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 3293 | /* check for empty pages with bitflips */ |
| 3294 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 3295 | &ecc_code[i], eccbytes, |
| 3296 | NULL, 0, |
| 3297 | chip->ecc.strength); |
| 3298 | } |
| 3299 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3300 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3301 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3302 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3303 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3304 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 3305 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3306 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3307 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3308 | } |
| 3309 | |
| 3310 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3311 | * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3312 | * @chip: nand chip info structure |
| 3313 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3314 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3315 | * @page: page number to read |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3316 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3317 | * Hardware ECC for large page chips, require OOB to be read first. For this |
| 3318 | * ECC mode, the write_page method is re-used from ECC_HW. These methods |
| 3319 | * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with |
| 3320 | * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from |
| 3321 | * the data area, by overwriting the NAND manufacturer bad block markings. |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3322 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3323 | static int nand_read_page_hwecc_oob_first(struct nand_chip *chip, uint8_t *buf, |
| 3324 | int oob_required, int page) |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3325 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3326 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3327 | int i, eccsize = chip->ecc.size, ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3328 | int eccbytes = chip->ecc.bytes; |
| 3329 | int eccsteps = chip->ecc.steps; |
| 3330 | uint8_t *p = buf; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3331 | uint8_t *ecc_code = chip->ecc.code_buf; |
| 3332 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3333 | unsigned int max_bitflips = 0; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3334 | |
| 3335 | /* Read the OOB area first */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3336 | ret = nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); |
| 3337 | if (ret) |
| 3338 | return ret; |
| 3339 | |
| 3340 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 3341 | if (ret) |
| 3342 | return ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3343 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3344 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 3345 | chip->ecc.total); |
| 3346 | if (ret) |
| 3347 | return ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3348 | |
| 3349 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 3350 | int stat; |
| 3351 | |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 3352 | chip->ecc.hwctl(chip, NAND_ECC_READ); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3353 | |
| 3354 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 3355 | if (ret) |
| 3356 | return ret; |
| 3357 | |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 3358 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3359 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 3360 | stat = chip->ecc.correct(chip, p, &ecc_code[i], NULL); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 3361 | if (stat == -EBADMSG && |
| 3362 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 3363 | /* check for empty pages with bitflips */ |
| 3364 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 3365 | &ecc_code[i], eccbytes, |
| 3366 | NULL, 0, |
| 3367 | chip->ecc.strength); |
| 3368 | } |
| 3369 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3370 | if (stat < 0) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3371 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3372 | } else { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3373 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3374 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 3375 | } |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3376 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3377 | return max_bitflips; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 3378 | } |
| 3379 | |
| 3380 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3381 | * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3382 | * @chip: nand chip info structure |
| 3383 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3384 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3385 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3386 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3387 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 3388 | * need a special oob layout and handling. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3389 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3390 | static int nand_read_page_syndrome(struct nand_chip *chip, uint8_t *buf, |
| 3391 | int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3392 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3393 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3394 | int ret, i, eccsize = chip->ecc.size; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3395 | int eccbytes = chip->ecc.bytes; |
| 3396 | int eccsteps = chip->ecc.steps; |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 3397 | int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3398 | uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3399 | uint8_t *oob = chip->oob_poi; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3400 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3401 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 3402 | ret = nand_read_page_op(chip, page, 0, NULL, 0); |
| 3403 | if (ret) |
| 3404 | return ret; |
| 3405 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3406 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 3407 | int stat; |
| 3408 | |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 3409 | chip->ecc.hwctl(chip, NAND_ECC_READ); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3410 | |
| 3411 | ret = nand_read_data_op(chip, p, eccsize, false); |
| 3412 | if (ret) |
| 3413 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3414 | |
| 3415 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3416 | ret = nand_read_data_op(chip, oob, chip->ecc.prepad, |
| 3417 | false); |
| 3418 | if (ret) |
| 3419 | return ret; |
| 3420 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3421 | oob += chip->ecc.prepad; |
| 3422 | } |
| 3423 | |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 3424 | chip->ecc.hwctl(chip, NAND_ECC_READSYN); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3425 | |
| 3426 | ret = nand_read_data_op(chip, oob, eccbytes, false); |
| 3427 | if (ret) |
| 3428 | return ret; |
| 3429 | |
Boris Brezillon | 00da2ea | 2018-09-06 14:05:19 +0200 | [diff] [blame] | 3430 | stat = chip->ecc.correct(chip, p, oob, NULL); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3431 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3432 | oob += eccbytes; |
| 3433 | |
| 3434 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3435 | ret = nand_read_data_op(chip, oob, chip->ecc.postpad, |
| 3436 | false); |
| 3437 | if (ret) |
| 3438 | return ret; |
| 3439 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3440 | oob += chip->ecc.postpad; |
| 3441 | } |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 3442 | |
| 3443 | if (stat == -EBADMSG && |
| 3444 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 3445 | /* check for empty pages with bitflips */ |
| 3446 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 3447 | oob - eccpadbytes, |
| 3448 | eccpadbytes, |
| 3449 | NULL, 0, |
| 3450 | chip->ecc.strength); |
| 3451 | } |
| 3452 | |
| 3453 | if (stat < 0) { |
| 3454 | mtd->ecc_stats.failed++; |
| 3455 | } else { |
| 3456 | mtd->ecc_stats.corrected += stat; |
| 3457 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 3458 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3459 | } |
| 3460 | |
| 3461 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 3462 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3463 | if (i) { |
| 3464 | ret = nand_read_data_op(chip, oob, i, false); |
| 3465 | if (ret) |
| 3466 | return ret; |
| 3467 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3468 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3469 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3470 | } |
| 3471 | |
| 3472 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3473 | * nand_transfer_oob - [INTERN] Transfer oob to client buffer |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3474 | * @mtd: mtd info structure |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3475 | * @oob: oob destination address |
| 3476 | * @ops: oob ops structure |
| 3477 | * @len: size of oob to transfer |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3478 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3479 | 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] | 3480 | struct mtd_oob_ops *ops, size_t len) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3481 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3482 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 3483 | int ret; |
| 3484 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3485 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3486 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3487 | case MTD_OPS_PLACE_OOB: |
| 3488 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3489 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); |
| 3490 | return oob + len; |
| 3491 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3492 | case MTD_OPS_AUTO_OOB: |
| 3493 | ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi, |
| 3494 | ops->ooboffs, len); |
| 3495 | BUG_ON(ret); |
| 3496 | return oob + len; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3497 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3498 | default: |
| 3499 | BUG(); |
| 3500 | } |
| 3501 | return NULL; |
| 3502 | } |
| 3503 | |
| 3504 | /** |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3505 | * nand_setup_read_retry - [INTERN] Set the READ RETRY mode |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 3506 | * @chip: NAND chip object |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3507 | * @retry_mode: the retry mode to use |
| 3508 | * |
| 3509 | * Some vendors supply a special command to shift the Vt threshold, to be used |
| 3510 | * when there are too many bitflips in a page (i.e., ECC error). After setting |
| 3511 | * a new threshold, the host should retry reading the page. |
| 3512 | */ |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 3513 | static int nand_setup_read_retry(struct nand_chip *chip, int retry_mode) |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3514 | { |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3515 | pr_debug("setting READ RETRY mode %d\n", retry_mode); |
| 3516 | |
| 3517 | if (retry_mode >= chip->read_retries) |
| 3518 | return -EINVAL; |
| 3519 | |
| 3520 | if (!chip->setup_read_retry) |
| 3521 | return -EOPNOTSUPP; |
| 3522 | |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 3523 | return chip->setup_read_retry(chip, retry_mode); |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3524 | } |
| 3525 | |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3526 | static void nand_wait_readrdy(struct nand_chip *chip) |
| 3527 | { |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 3528 | const struct nand_sdr_timings *sdr; |
| 3529 | |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3530 | if (!(chip->options & NAND_NEED_READRDY)) |
| 3531 | return; |
| 3532 | |
Boris Brezillon | 52f05b6 | 2018-07-27 09:44:18 +0200 | [diff] [blame] | 3533 | sdr = nand_get_sdr_timings(&chip->data_interface); |
| 3534 | WARN_ON(nand_wait_rdy_op(chip, PSEC_TO_MSEC(sdr->tR_max), 0)); |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3535 | } |
| 3536 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3537 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3538 | * nand_do_read_ops - [INTERN] Read data with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3539 | * @mtd: MTD device structure |
| 3540 | * @from: offset to read from |
| 3541 | * @ops: oob ops structure |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 3542 | * |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3543 | * Internal function. Called with chip held. |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 3544 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3545 | static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, |
| 3546 | struct mtd_oob_ops *ops) |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 3547 | { |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3548 | int chipnr, page, realpage, col, bytes, aligned, oob_required; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3549 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3550 | int ret = 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3551 | uint32_t readlen = ops->len; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3552 | uint32_t oobreadlen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 3553 | uint32_t max_oobsize = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 9aca334 | 2010-02-22 20:39:35 +0200 | [diff] [blame] | 3554 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3555 | uint8_t *bufpoi, *oob, *buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3556 | int use_bufpoi; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3557 | unsigned int max_bitflips = 0; |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3558 | int retry_mode = 0; |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3559 | bool ecc_fail = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3560 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3561 | chipnr = (int)(from >> chip->chip_shift); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3562 | chip->select_chip(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3563 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3564 | realpage = (int)(from >> chip->page_shift); |
| 3565 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3566 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3567 | col = (int)(from & (mtd->writesize - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3568 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3569 | buf = ops->datbuf; |
| 3570 | oob = ops->oobbuf; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3571 | oob_required = oob ? 1 : 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3572 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3573 | while (1) { |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3574 | unsigned int ecc_failures = mtd->ecc_stats.failed; |
| 3575 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3576 | bytes = min(mtd->writesize - col, readlen); |
| 3577 | aligned = (bytes == mtd->writesize); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3578 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3579 | if (!aligned) |
| 3580 | use_bufpoi = 1; |
| 3581 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 3582 | use_bufpoi = !virt_addr_valid(buf) || |
| 3583 | !IS_ALIGNED((unsigned long)buf, |
| 3584 | chip->buf_align); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3585 | else |
| 3586 | use_bufpoi = 0; |
| 3587 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3588 | /* Is the current page in the buffer? */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3589 | if (realpage != chip->pagebuf || oob) { |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3590 | bufpoi = use_bufpoi ? chip->data_buf : buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3591 | |
| 3592 | if (use_bufpoi && aligned) |
| 3593 | pr_debug("%s: using read bounce buffer for buf@%p\n", |
| 3594 | __func__, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3595 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3596 | read_retry: |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3597 | /* |
| 3598 | * Now read the page into the buffer. Absent an error, |
| 3599 | * the read methods return max bitflips per ecc step. |
| 3600 | */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3601 | if (unlikely(ops->mode == MTD_OPS_RAW)) |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3602 | ret = chip->ecc.read_page_raw(chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3603 | oob_required, |
| 3604 | page); |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 3605 | else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) && |
| 3606 | !oob) |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3607 | ret = chip->ecc.read_subpage(chip, col, bytes, |
| 3608 | bufpoi, page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 3609 | else |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3610 | ret = chip->ecc.read_page(chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 3611 | oob_required, page); |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3612 | if (ret < 0) { |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3613 | if (use_bufpoi) |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3614 | /* Invalidate page cache */ |
| 3615 | chip->pagebuf = -1; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3616 | break; |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3617 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3618 | |
| 3619 | /* Transfer not aligned data */ |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 3620 | if (use_bufpoi) { |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 3621 | if (!NAND_HAS_SUBPAGE_READ(chip) && !oob && |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3622 | !(mtd->ecc_stats.failed - ecc_failures) && |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3623 | (ops->mode != MTD_OPS_RAW)) { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 3624 | chip->pagebuf = realpage; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3625 | chip->pagebuf_bitflips = ret; |
| 3626 | } else { |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 3627 | /* Invalidate page cache */ |
| 3628 | chip->pagebuf = -1; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3629 | } |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3630 | memcpy(buf, chip->data_buf + col, bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3631 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3632 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3633 | if (unlikely(oob)) { |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 3634 | int toread = min(oobreadlen, max_oobsize); |
| 3635 | |
| 3636 | if (toread) { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3637 | oob = nand_transfer_oob(mtd, |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 3638 | oob, ops, toread); |
| 3639 | oobreadlen -= toread; |
| 3640 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3641 | } |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 3642 | |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3643 | nand_wait_readrdy(chip); |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3644 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3645 | if (mtd->ecc_stats.failed - ecc_failures) { |
Brian Norris | 28fa65e | 2014-02-12 16:08:28 -0800 | [diff] [blame] | 3646 | if (retry_mode + 1 < chip->read_retries) { |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3647 | retry_mode++; |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 3648 | ret = nand_setup_read_retry(chip, |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3649 | retry_mode); |
| 3650 | if (ret < 0) |
| 3651 | break; |
| 3652 | |
| 3653 | /* Reset failures; retry */ |
| 3654 | mtd->ecc_stats.failed = ecc_failures; |
| 3655 | goto read_retry; |
| 3656 | } else { |
| 3657 | /* No more retry modes; real failure */ |
| 3658 | ecc_fail = true; |
| 3659 | } |
| 3660 | } |
| 3661 | |
| 3662 | buf += bytes; |
Masahiro Yamada | 0760468 | 2017-03-30 15:45:47 +0900 | [diff] [blame] | 3663 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3664 | } else { |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 3665 | memcpy(buf, chip->data_buf + col, bytes); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3666 | buf += bytes; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3667 | max_bitflips = max_t(unsigned int, max_bitflips, |
| 3668 | chip->pagebuf_bitflips); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3669 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3670 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3671 | readlen -= bytes; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3672 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3673 | /* Reset to retry mode 0 */ |
| 3674 | if (retry_mode) { |
Boris Brezillon | 2e7f1ce | 2018-09-06 14:05:32 +0200 | [diff] [blame] | 3675 | ret = nand_setup_read_retry(chip, 0); |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 3676 | if (ret < 0) |
| 3677 | break; |
| 3678 | retry_mode = 0; |
| 3679 | } |
| 3680 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3681 | if (!readlen) |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3682 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3683 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3684 | /* For subsequent reads align to page boundary */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3685 | col = 0; |
| 3686 | /* Increment page address */ |
| 3687 | realpage++; |
| 3688 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3689 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3690 | /* Check, if we cross a chip boundary */ |
| 3691 | if (!page) { |
| 3692 | chipnr++; |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3693 | chip->select_chip(chip, -1); |
| 3694 | chip->select_chip(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3695 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3696 | } |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3697 | chip->select_chip(chip, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3698 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3699 | ops->retlen = ops->len - (size_t) readlen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3700 | if (oob) |
| 3701 | ops->oobretlen = ops->ooblen - oobreadlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3702 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 3703 | if (ret < 0) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3704 | return ret; |
| 3705 | |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 3706 | if (ecc_fail) |
Thomas Gleixner | 9a1fcdf | 2006-05-29 14:56:39 +0200 | [diff] [blame] | 3707 | return -EBADMSG; |
| 3708 | |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 3709 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 3710 | } |
| 3711 | |
| 3712 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3713 | * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3714 | * @chip: nand chip info structure |
| 3715 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3716 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3717 | int nand_read_oob_std(struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3718 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3719 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 3720 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3721 | return nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3722 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 3723 | EXPORT_SYMBOL(nand_read_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3724 | |
| 3725 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3726 | * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3727 | * with syndromes |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3728 | * @chip: nand chip info structure |
| 3729 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3730 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3731 | int nand_read_oob_syndrome(struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3732 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3733 | struct mtd_info *mtd = nand_to_mtd(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3734 | int length = mtd->oobsize; |
| 3735 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 3736 | int eccsize = chip->ecc.size; |
Baruch Siach | 2ea69d2 | 2015-01-22 15:23:05 +0200 | [diff] [blame] | 3737 | uint8_t *bufpoi = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3738 | int i, toread, sndrnd = 0, pos, ret; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3739 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3740 | ret = nand_read_page_op(chip, page, chip->ecc.size, NULL, 0); |
| 3741 | if (ret) |
| 3742 | return ret; |
| 3743 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3744 | for (i = 0; i < chip->ecc.steps; i++) { |
| 3745 | if (sndrnd) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3746 | int ret; |
| 3747 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3748 | pos = eccsize + i * (eccsize + chunk); |
| 3749 | if (mtd->writesize > 512) |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3750 | ret = nand_change_read_column_op(chip, pos, |
| 3751 | NULL, 0, |
| 3752 | false); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3753 | else |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3754 | ret = nand_read_page_op(chip, page, pos, NULL, |
| 3755 | 0); |
| 3756 | |
| 3757 | if (ret) |
| 3758 | return ret; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3759 | } else |
| 3760 | sndrnd = 1; |
| 3761 | toread = min_t(int, length, chunk); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3762 | |
| 3763 | ret = nand_read_data_op(chip, bufpoi, toread, false); |
| 3764 | if (ret) |
| 3765 | return ret; |
| 3766 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3767 | bufpoi += toread; |
| 3768 | length -= toread; |
| 3769 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3770 | if (length > 0) { |
| 3771 | ret = nand_read_data_op(chip, bufpoi, length, false); |
| 3772 | if (ret) |
| 3773 | return ret; |
| 3774 | } |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3775 | |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 3776 | return 0; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3777 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 3778 | EXPORT_SYMBOL(nand_read_oob_syndrome); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3779 | |
| 3780 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3781 | * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3782 | * @chip: nand chip info structure |
| 3783 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3784 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3785 | int nand_write_oob_std(struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3786 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3787 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 3788 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3789 | return nand_prog_page_op(chip, page, mtd->writesize, chip->oob_poi, |
| 3790 | mtd->oobsize); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3791 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 3792 | EXPORT_SYMBOL(nand_write_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3793 | |
| 3794 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3795 | * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3796 | * with syndrome - only for large page flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3797 | * @chip: nand chip info structure |
| 3798 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3799 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3800 | int nand_write_oob_syndrome(struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3801 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3802 | struct mtd_info *mtd = nand_to_mtd(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3803 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 3804 | int eccsize = chip->ecc.size, length = mtd->oobsize; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3805 | int ret, i, len, pos, sndcmd = 0, steps = chip->ecc.steps; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3806 | const uint8_t *bufpoi = chip->oob_poi; |
| 3807 | |
| 3808 | /* |
| 3809 | * data-ecc-data-ecc ... ecc-oob |
| 3810 | * or |
| 3811 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob |
| 3812 | */ |
| 3813 | if (!chip->ecc.prepad && !chip->ecc.postpad) { |
| 3814 | pos = steps * (eccsize + chunk); |
| 3815 | steps = 0; |
| 3816 | } else |
Vitaly Wool | 8b0036e | 2006-07-11 09:11:25 +0200 | [diff] [blame] | 3817 | pos = eccsize; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3818 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3819 | ret = nand_prog_page_begin_op(chip, page, pos, NULL, 0); |
| 3820 | if (ret) |
| 3821 | return ret; |
| 3822 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3823 | for (i = 0; i < steps; i++) { |
| 3824 | if (sndcmd) { |
| 3825 | if (mtd->writesize <= 512) { |
| 3826 | uint32_t fill = 0xFFFFFFFF; |
| 3827 | |
| 3828 | len = eccsize; |
| 3829 | while (len > 0) { |
| 3830 | int num = min_t(int, len, 4); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3831 | |
| 3832 | ret = nand_write_data_op(chip, &fill, |
| 3833 | num, false); |
| 3834 | if (ret) |
| 3835 | return ret; |
| 3836 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3837 | len -= num; |
| 3838 | } |
| 3839 | } else { |
| 3840 | pos = eccsize + i * (eccsize + chunk); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3841 | ret = nand_change_write_column_op(chip, pos, |
| 3842 | NULL, 0, |
| 3843 | false); |
| 3844 | if (ret) |
| 3845 | return ret; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3846 | } |
| 3847 | } else |
| 3848 | sndcmd = 1; |
| 3849 | len = min_t(int, length, chunk); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3850 | |
| 3851 | ret = nand_write_data_op(chip, bufpoi, len, false); |
| 3852 | if (ret) |
| 3853 | return ret; |
| 3854 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3855 | bufpoi += len; |
| 3856 | length -= len; |
| 3857 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3858 | if (length > 0) { |
| 3859 | ret = nand_write_data_op(chip, bufpoi, length, false); |
| 3860 | if (ret) |
| 3861 | return ret; |
| 3862 | } |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3863 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 3864 | return nand_prog_page_end_op(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3865 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 3866 | EXPORT_SYMBOL(nand_write_oob_syndrome); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3867 | |
| 3868 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3869 | * nand_do_read_oob - [INTERN] NAND read out-of-band |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3870 | * @mtd: MTD device structure |
| 3871 | * @from: offset to read from |
| 3872 | * @ops: oob operations description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3873 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3874 | * NAND read out-of-band data from the spare area. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3875 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3876 | static int nand_do_read_oob(struct mtd_info *mtd, loff_t from, |
| 3877 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3878 | { |
Miquel Raynal | 87e89ce | 2018-01-12 10:13:36 +0100 | [diff] [blame] | 3879 | unsigned int max_bitflips = 0; |
Brian Norris | c00a099 | 2012-05-01 17:12:54 -0700 | [diff] [blame] | 3880 | int page, realpage, chipnr; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3881 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 3882 | struct mtd_ecc_stats stats; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3883 | int readlen = ops->ooblen; |
| 3884 | int len; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3885 | uint8_t *buf = ops->oobbuf; |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 3886 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3887 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3888 | pr_debug("%s: from = 0x%08Lx, len = %i\n", |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 3889 | __func__, (unsigned long long)from, readlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3890 | |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 3891 | stats = mtd->ecc_stats; |
| 3892 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 3893 | len = mtd_oobavail(mtd, ops); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 3894 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3895 | chipnr = (int)(from >> chip->chip_shift); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3896 | chip->select_chip(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3897 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3898 | /* Shift to get page */ |
| 3899 | realpage = (int)(from >> chip->page_shift); |
| 3900 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3901 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3902 | while (1) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3903 | if (ops->mode == MTD_OPS_RAW) |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3904 | ret = chip->ecc.read_oob_raw(chip, page); |
Brian Norris | c46f648 | 2011-08-30 18:45:38 -0700 | [diff] [blame] | 3905 | else |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 3906 | ret = chip->ecc.read_oob(chip, page); |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 3907 | |
| 3908 | if (ret < 0) |
| 3909 | break; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3910 | |
| 3911 | len = min(len, readlen); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 3912 | buf = nand_transfer_oob(mtd, buf, ops, len); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3913 | |
Boris Brezillon | 85e08e5 | 2018-07-27 09:44:17 +0200 | [diff] [blame] | 3914 | nand_wait_readrdy(chip); |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 3915 | |
Miquel Raynal | 87e89ce | 2018-01-12 10:13:36 +0100 | [diff] [blame] | 3916 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
| 3917 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3918 | readlen -= len; |
Savin Zlobec | 0d420f9 | 2006-06-21 11:51:20 +0200 | [diff] [blame] | 3919 | if (!readlen) |
| 3920 | break; |
| 3921 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3922 | /* Increment page address */ |
| 3923 | realpage++; |
| 3924 | |
| 3925 | page = realpage & chip->pagemask; |
| 3926 | /* Check, if we cross a chip boundary */ |
| 3927 | if (!page) { |
| 3928 | chipnr++; |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3929 | chip->select_chip(chip, -1); |
| 3930 | chip->select_chip(chip, chipnr); |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 3931 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3932 | } |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 3933 | chip->select_chip(chip, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3934 | |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 3935 | ops->oobretlen = ops->ooblen - readlen; |
| 3936 | |
| 3937 | if (ret < 0) |
| 3938 | return ret; |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 3939 | |
| 3940 | if (mtd->ecc_stats.failed - stats.failed) |
| 3941 | return -EBADMSG; |
| 3942 | |
Miquel Raynal | 87e89ce | 2018-01-12 10:13:36 +0100 | [diff] [blame] | 3943 | return max_bitflips; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3944 | } |
| 3945 | |
| 3946 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3947 | * 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] | 3948 | * @mtd: MTD device structure |
| 3949 | * @from: offset to read from |
| 3950 | * @ops: oob operation description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3951 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3952 | * NAND read data and/or out-of-band data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3953 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3954 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
| 3955 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3956 | { |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 3957 | int ret; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3958 | |
| 3959 | ops->retlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3960 | |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 3961 | if (ops->mode != MTD_OPS_PLACE_OOB && |
| 3962 | ops->mode != MTD_OPS_AUTO_OOB && |
| 3963 | ops->mode != MTD_OPS_RAW) |
| 3964 | return -ENOTSUPP; |
| 3965 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3966 | nand_get_device(mtd, FL_READING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3967 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3968 | if (!ops->datbuf) |
| 3969 | ret = nand_do_read_oob(mtd, from, ops); |
| 3970 | else |
| 3971 | ret = nand_do_read_ops(mtd, from, ops); |
| 3972 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3973 | nand_release_device(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3974 | return ret; |
| 3975 | } |
| 3976 | |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 3977 | /** |
| 3978 | * nand_write_page_raw_notsupp - dummy raw page write function |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 3979 | * @chip: nand chip info structure |
| 3980 | * @buf: data buffer |
| 3981 | * @oob_required: must write chip->oob_poi to OOB |
| 3982 | * @page: page number to write |
| 3983 | * |
| 3984 | * Returns -ENOTSUPP unconditionally. |
| 3985 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 3986 | int nand_write_page_raw_notsupp(struct nand_chip *chip, const u8 *buf, |
| 3987 | int oob_required, int page) |
Boris Brezillon | 0d6030a | 2018-07-18 10:42:17 +0200 | [diff] [blame] | 3988 | { |
| 3989 | return -ENOTSUPP; |
| 3990 | } |
| 3991 | EXPORT_SYMBOL(nand_write_page_raw_notsupp); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3992 | |
| 3993 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3994 | * nand_write_page_raw - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3995 | * @chip: nand chip info structure |
| 3996 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 3997 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 3998 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 3999 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 4000 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4001 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4002 | int nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf, |
| 4003 | int oob_required, int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4004 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4005 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4006 | int ret; |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 4007 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 4008 | ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4009 | if (ret) |
| 4010 | return ret; |
| 4011 | |
| 4012 | if (oob_required) { |
| 4013 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, |
| 4014 | false); |
| 4015 | if (ret) |
| 4016 | return ret; |
| 4017 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 4018 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 4019 | return nand_prog_page_end_op(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4020 | } |
Thomas Petazzoni | cc0f51e | 2017-04-29 11:06:44 +0200 | [diff] [blame] | 4021 | EXPORT_SYMBOL(nand_write_page_raw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4022 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4023 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 4024 | * nand_write_page_raw_syndrome - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4025 | * @chip: nand chip info structure |
| 4026 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 4027 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 4028 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 4029 | * |
| 4030 | * We need a special oob layout and handling even when ECC isn't checked. |
| 4031 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4032 | static int nand_write_page_raw_syndrome(struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 4033 | const uint8_t *buf, int oob_required, |
| 4034 | int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 4035 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4036 | struct mtd_info *mtd = nand_to_mtd(chip); |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 4037 | int eccsize = chip->ecc.size; |
| 4038 | int eccbytes = chip->ecc.bytes; |
| 4039 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4040 | int steps, size, ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 4041 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 4042 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 4043 | if (ret) |
| 4044 | return ret; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 4045 | |
| 4046 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4047 | ret = nand_write_data_op(chip, buf, eccsize, false); |
| 4048 | if (ret) |
| 4049 | return ret; |
| 4050 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 4051 | buf += eccsize; |
| 4052 | |
| 4053 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4054 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 4055 | false); |
| 4056 | if (ret) |
| 4057 | return ret; |
| 4058 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 4059 | oob += chip->ecc.prepad; |
| 4060 | } |
| 4061 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4062 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 4063 | if (ret) |
| 4064 | return ret; |
| 4065 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 4066 | oob += eccbytes; |
| 4067 | |
| 4068 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4069 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 4070 | false); |
| 4071 | if (ret) |
| 4072 | return ret; |
| 4073 | |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 4074 | oob += chip->ecc.postpad; |
| 4075 | } |
| 4076 | } |
| 4077 | |
| 4078 | size = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4079 | if (size) { |
| 4080 | ret = nand_write_data_op(chip, oob, size, false); |
| 4081 | if (ret) |
| 4082 | return ret; |
| 4083 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 4084 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 4085 | return nand_prog_page_end_op(chip); |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 4086 | } |
| 4087 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 4088 | * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4089 | * @chip: nand chip info structure |
| 4090 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 4091 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 4092 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4093 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4094 | static int nand_write_page_swecc(struct nand_chip *chip, const uint8_t *buf, |
| 4095 | int oob_required, int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4096 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4097 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4098 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4099 | int eccbytes = chip->ecc.bytes; |
| 4100 | int eccsteps = chip->ecc.steps; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 4101 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4102 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4103 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 4104 | /* Software ECC calculation */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4105 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 4106 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4107 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4108 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 4109 | chip->ecc.total); |
| 4110 | if (ret) |
| 4111 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4112 | |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4113 | return chip->ecc.write_page_raw(chip, buf, 1, page); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4114 | } |
| 4115 | |
| 4116 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 4117 | * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4118 | * @chip: nand chip info structure |
| 4119 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 4120 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 4121 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4122 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4123 | static int nand_write_page_hwecc(struct nand_chip *chip, const uint8_t *buf, |
| 4124 | int oob_required, int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4125 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4126 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4127 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4128 | int eccbytes = chip->ecc.bytes; |
| 4129 | int eccsteps = chip->ecc.steps; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 4130 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4131 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4132 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 4133 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 4134 | if (ret) |
| 4135 | return ret; |
| 4136 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4137 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 4138 | chip->ecc.hwctl(chip, NAND_ECC_WRITE); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4139 | |
| 4140 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 4141 | if (ret) |
| 4142 | return ret; |
| 4143 | |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 4144 | chip->ecc.calculate(chip, p, &ecc_calc[i]); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4145 | } |
| 4146 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4147 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 4148 | chip->ecc.total); |
| 4149 | if (ret) |
| 4150 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4151 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4152 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 4153 | if (ret) |
| 4154 | return ret; |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 4155 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 4156 | return nand_prog_page_end_op(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4157 | } |
| 4158 | |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4159 | |
| 4160 | /** |
Brian Norris | 73c8aaf | 2015-02-28 02:04:18 -0800 | [diff] [blame] | 4161 | * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4162 | * @chip: nand chip info structure |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 4163 | * @offset: column address of subpage within the page |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4164 | * @data_len: data length |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 4165 | * @buf: data buffer |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4166 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 4167 | * @page: page number to write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4168 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4169 | static int nand_write_subpage_hwecc(struct nand_chip *chip, uint32_t offset, |
| 4170 | uint32_t data_len, const uint8_t *buf, |
| 4171 | int oob_required, int page) |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4172 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4173 | struct mtd_info *mtd = nand_to_mtd(chip); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4174 | uint8_t *oob_buf = chip->oob_poi; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 4175 | uint8_t *ecc_calc = chip->ecc.calc_buf; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4176 | int ecc_size = chip->ecc.size; |
| 4177 | int ecc_bytes = chip->ecc.bytes; |
| 4178 | int ecc_steps = chip->ecc.steps; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4179 | uint32_t start_step = offset / ecc_size; |
| 4180 | uint32_t end_step = (offset + data_len - 1) / ecc_size; |
| 4181 | int oob_bytes = mtd->oobsize / ecc_steps; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4182 | int step, ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4183 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 4184 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 4185 | if (ret) |
| 4186 | return ret; |
| 4187 | |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4188 | for (step = 0; step < ecc_steps; step++) { |
| 4189 | /* configure controller for WRITE access */ |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 4190 | chip->ecc.hwctl(chip, NAND_ECC_WRITE); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4191 | |
| 4192 | /* write data (untouched subpages already masked by 0xFF) */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4193 | ret = nand_write_data_op(chip, buf, ecc_size, false); |
| 4194 | if (ret) |
| 4195 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4196 | |
| 4197 | /* mask ECC of un-touched subpages by padding 0xFF */ |
| 4198 | if ((step < start_step) || (step > end_step)) |
| 4199 | memset(ecc_calc, 0xff, ecc_bytes); |
| 4200 | else |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 4201 | chip->ecc.calculate(chip, buf, ecc_calc); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4202 | |
| 4203 | /* mask OOB of un-touched subpages by padding 0xFF */ |
| 4204 | /* if oob_required, preserve OOB metadata of written subpage */ |
| 4205 | if (!oob_required || (step < start_step) || (step > end_step)) |
| 4206 | memset(oob_buf, 0xff, oob_bytes); |
| 4207 | |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 4208 | buf += ecc_size; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4209 | ecc_calc += ecc_bytes; |
| 4210 | oob_buf += oob_bytes; |
| 4211 | } |
| 4212 | |
| 4213 | /* copy calculated ECC for whole page to chip->buffer->oob */ |
| 4214 | /* this include masked-value(0xFF) for unwritten subpages */ |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 4215 | ecc_calc = chip->ecc.calc_buf; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4216 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 4217 | chip->ecc.total); |
| 4218 | if (ret) |
| 4219 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4220 | |
| 4221 | /* write OOB buffer to NAND device */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4222 | ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize, false); |
| 4223 | if (ret) |
| 4224 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4225 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 4226 | return nand_prog_page_end_op(chip); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4227 | } |
| 4228 | |
| 4229 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4230 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 4231 | * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4232 | * @chip: nand chip info structure |
| 4233 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 4234 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 4235 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4236 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4237 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 4238 | * need a special oob layout and handling. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4239 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4240 | static int nand_write_page_syndrome(struct nand_chip *chip, const uint8_t *buf, |
| 4241 | int oob_required, int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4242 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4243 | struct mtd_info *mtd = nand_to_mtd(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4244 | int i, eccsize = chip->ecc.size; |
| 4245 | int eccbytes = chip->ecc.bytes; |
| 4246 | int eccsteps = chip->ecc.steps; |
| 4247 | const uint8_t *p = buf; |
| 4248 | uint8_t *oob = chip->oob_poi; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4249 | int ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4250 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 4251 | ret = nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 4252 | if (ret) |
| 4253 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4254 | |
| 4255 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
Boris Brezillon | ec47636 | 2018-09-06 14:05:17 +0200 | [diff] [blame] | 4256 | chip->ecc.hwctl(chip, NAND_ECC_WRITE); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4257 | |
| 4258 | ret = nand_write_data_op(chip, p, eccsize, false); |
| 4259 | if (ret) |
| 4260 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4261 | |
| 4262 | if (chip->ecc.prepad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4263 | ret = nand_write_data_op(chip, oob, chip->ecc.prepad, |
| 4264 | false); |
| 4265 | if (ret) |
| 4266 | return ret; |
| 4267 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4268 | oob += chip->ecc.prepad; |
| 4269 | } |
| 4270 | |
Boris Brezillon | af37d2c | 2018-09-06 14:05:18 +0200 | [diff] [blame] | 4271 | chip->ecc.calculate(chip, p, oob); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4272 | |
| 4273 | ret = nand_write_data_op(chip, oob, eccbytes, false); |
| 4274 | if (ret) |
| 4275 | return ret; |
| 4276 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4277 | oob += eccbytes; |
| 4278 | |
| 4279 | if (chip->ecc.postpad) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4280 | ret = nand_write_data_op(chip, oob, chip->ecc.postpad, |
| 4281 | false); |
| 4282 | if (ret) |
| 4283 | return ret; |
| 4284 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4285 | oob += chip->ecc.postpad; |
| 4286 | } |
| 4287 | } |
| 4288 | |
| 4289 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 4290 | i = mtd->oobsize - (oob - chip->oob_poi); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4291 | if (i) { |
| 4292 | ret = nand_write_data_op(chip, oob, i, false); |
| 4293 | if (ret) |
| 4294 | return ret; |
| 4295 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 4296 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 4297 | return nand_prog_page_end_op(chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4298 | } |
| 4299 | |
| 4300 | /** |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 4301 | * nand_write_page - write one page |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4302 | * @mtd: MTD device structure |
| 4303 | * @chip: NAND chip descriptor |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4304 | * @offset: address offset within the page |
| 4305 | * @data_len: length of actual data to be written |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4306 | * @buf: the data to write |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 4307 | * @oob_required: must write chip->oob_poi to OOB |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4308 | * @page: page number to write |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4309 | * @raw: use _raw version of write_page |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4310 | */ |
| 4311 | 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] | 4312 | uint32_t offset, int data_len, const uint8_t *buf, |
Boris Brezillon | 0b4773fd | 2017-05-16 00:17:41 +0200 | [diff] [blame] | 4313 | int oob_required, int page, int raw) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4314 | { |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4315 | int status, subpage; |
| 4316 | |
| 4317 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && |
| 4318 | chip->ecc.write_subpage) |
| 4319 | subpage = offset || (data_len < mtd->writesize); |
| 4320 | else |
| 4321 | subpage = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4322 | |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 4323 | if (unlikely(raw)) |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4324 | status = chip->ecc.write_page_raw(chip, buf, oob_required, |
| 4325 | page); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4326 | else if (subpage) |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4327 | status = chip->ecc.write_subpage(chip, offset, data_len, buf, |
| 4328 | oob_required, page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 4329 | else |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4330 | status = chip->ecc.write_page(chip, buf, oob_required, page); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 4331 | |
| 4332 | if (status < 0) |
| 4333 | return status; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4334 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4335 | return 0; |
| 4336 | } |
| 4337 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4338 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 4339 | * nand_fill_oob - [INTERN] Transfer client buffer to oob |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 4340 | * @mtd: MTD device structure |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4341 | * @oob: oob data buffer |
| 4342 | * @len: oob data write length |
| 4343 | * @ops: oob ops structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4344 | */ |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 4345 | static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len, |
| 4346 | struct mtd_oob_ops *ops) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4347 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4348 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4349 | int ret; |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 4350 | |
| 4351 | /* |
| 4352 | * Initialise to all 0xFF, to avoid the possibility of left over OOB |
| 4353 | * data from a previous OOB read. |
| 4354 | */ |
| 4355 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
| 4356 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 4357 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4358 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 4359 | case MTD_OPS_PLACE_OOB: |
| 4360 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4361 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); |
| 4362 | return oob + len; |
| 4363 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4364 | case MTD_OPS_AUTO_OOB: |
| 4365 | ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi, |
| 4366 | ops->ooboffs, len); |
| 4367 | BUG_ON(ret); |
| 4368 | return oob + len; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4369 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4370 | default: |
| 4371 | BUG(); |
| 4372 | } |
| 4373 | return NULL; |
| 4374 | } |
| 4375 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 4376 | #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4377 | |
| 4378 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 4379 | * nand_do_write_ops - [INTERN] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4380 | * @mtd: MTD device structure |
| 4381 | * @to: offset to write to |
| 4382 | * @ops: oob operations description structure |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4383 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4384 | * NAND write with ECC. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4385 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4386 | static int nand_do_write_ops(struct mtd_info *mtd, loff_t to, |
| 4387 | struct mtd_oob_ops *ops) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4388 | { |
Corentin Labbe | 73600b6 | 2017-09-02 10:49:38 +0200 | [diff] [blame] | 4389 | int chipnr, realpage, page, column; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4390 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4391 | uint32_t writelen = ops->len; |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4392 | |
| 4393 | uint32_t oobwritelen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 4394 | uint32_t oobmaxlen = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4395 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4396 | uint8_t *oob = ops->oobbuf; |
| 4397 | uint8_t *buf = ops->datbuf; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 4398 | int ret; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 4399 | int oob_required = oob ? 1 : 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4400 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4401 | ops->retlen = 0; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4402 | if (!writelen) |
| 4403 | return 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4404 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4405 | /* Reject writes, which are not page aligned */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4406 | if (NOTALIGNED(to) || NOTALIGNED(ops->len)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 4407 | pr_notice("%s: attempt to write non page aligned data\n", |
| 4408 | __func__); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4409 | return -EINVAL; |
| 4410 | } |
| 4411 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4412 | column = to & (mtd->writesize - 1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4413 | |
Thomas Gleixner | 6a93096 | 2006-06-28 00:11:45 +0200 | [diff] [blame] | 4414 | chipnr = (int)(to >> chip->chip_shift); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4415 | chip->select_chip(chip, chipnr); |
Thomas Gleixner | 6a93096 | 2006-06-28 00:11:45 +0200 | [diff] [blame] | 4416 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4417 | /* Check, if it is write protected */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 4418 | if (nand_check_wp(mtd)) { |
| 4419 | ret = -EIO; |
| 4420 | goto err_out; |
| 4421 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4422 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4423 | realpage = (int)(to >> chip->page_shift); |
| 4424 | page = realpage & chip->pagemask; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4425 | |
| 4426 | /* Invalidate the page cache, when we write to the cached page */ |
Brian Norris | 537ab1b | 2014-07-21 19:08:03 -0700 | [diff] [blame] | 4427 | if (to <= ((loff_t)chip->pagebuf << chip->page_shift) && |
| 4428 | ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len)) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4429 | chip->pagebuf = -1; |
| 4430 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4431 | /* Don't allow multipage oob writes with offset */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 4432 | if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) { |
| 4433 | ret = -EINVAL; |
| 4434 | goto err_out; |
| 4435 | } |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4436 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 4437 | while (1) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4438 | int bytes = mtd->writesize; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4439 | uint8_t *wbuf = buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 4440 | int use_bufpoi; |
Hector Palacios | 144f4c9 | 2016-07-18 10:39:18 +0200 | [diff] [blame] | 4441 | int part_pagewr = (column || writelen < mtd->writesize); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4442 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 4443 | if (part_pagewr) |
| 4444 | use_bufpoi = 1; |
| 4445 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 4446 | use_bufpoi = !virt_addr_valid(buf) || |
| 4447 | !IS_ALIGNED((unsigned long)buf, |
| 4448 | chip->buf_align); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 4449 | else |
| 4450 | use_bufpoi = 0; |
| 4451 | |
| 4452 | /* Partial page write?, or need to use bounce buffer */ |
| 4453 | if (use_bufpoi) { |
| 4454 | pr_debug("%s: using write bounce buffer for buf@%p\n", |
| 4455 | __func__, buf); |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 4456 | if (part_pagewr) |
| 4457 | bytes = min_t(int, bytes - column, writelen); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4458 | chip->pagebuf = -1; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 4459 | memset(chip->data_buf, 0xff, mtd->writesize); |
| 4460 | memcpy(&chip->data_buf[column], buf, bytes); |
| 4461 | wbuf = chip->data_buf; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4462 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4463 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4464 | if (unlikely(oob)) { |
| 4465 | size_t len = min(oobwritelen, oobmaxlen); |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 4466 | oob = nand_fill_oob(mtd, oob, len, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4467 | oobwritelen -= len; |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 4468 | } else { |
| 4469 | /* We still need to erase leftover OOB data */ |
| 4470 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 4471 | } |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 4472 | |
| 4473 | ret = nand_write_page(mtd, chip, column, bytes, wbuf, |
Boris Brezillon | 0b4773fd | 2017-05-16 00:17:41 +0200 | [diff] [blame] | 4474 | oob_required, page, |
Boris Brezillon | f107d7a | 2017-03-16 09:02:42 +0100 | [diff] [blame] | 4475 | (ops->mode == MTD_OPS_RAW)); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4476 | if (ret) |
| 4477 | break; |
| 4478 | |
| 4479 | writelen -= bytes; |
| 4480 | if (!writelen) |
| 4481 | break; |
| 4482 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4483 | column = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4484 | buf += bytes; |
| 4485 | realpage++; |
| 4486 | |
| 4487 | page = realpage & chip->pagemask; |
| 4488 | /* Check, if we cross a chip boundary */ |
| 4489 | if (!page) { |
| 4490 | chipnr++; |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4491 | chip->select_chip(chip, -1); |
| 4492 | chip->select_chip(chip, chipnr); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4493 | } |
| 4494 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4495 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4496 | ops->retlen = ops->len - writelen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 4497 | if (unlikely(oob)) |
| 4498 | ops->oobretlen = ops->ooblen; |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 4499 | |
| 4500 | err_out: |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4501 | chip->select_chip(chip, -1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4502 | return ret; |
| 4503 | } |
| 4504 | |
| 4505 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4506 | * panic_nand_write - [MTD Interface] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4507 | * @mtd: MTD device structure |
| 4508 | * @to: offset to write to |
| 4509 | * @len: number of bytes to write |
| 4510 | * @retlen: pointer to variable to store the number of written bytes |
| 4511 | * @buf: the data to write |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4512 | * |
| 4513 | * NAND write with ECC. Used when performing writes in interrupt context, this |
| 4514 | * may for example be called by mtdoops when writing an oops while in panic. |
| 4515 | */ |
| 4516 | static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 4517 | size_t *retlen, const uint8_t *buf) |
| 4518 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4519 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brent Taylor | 30863e38 | 2017-10-30 22:32:45 -0500 | [diff] [blame] | 4520 | int chipnr = (int)(to >> chip->chip_shift); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 4521 | struct mtd_oob_ops ops; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4522 | int ret; |
| 4523 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4524 | /* Grab the device */ |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4525 | panic_nand_get_device(chip, mtd, FL_WRITING); |
| 4526 | |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4527 | chip->select_chip(chip, chipnr); |
Brent Taylor | 30863e38 | 2017-10-30 22:32:45 -0500 | [diff] [blame] | 4528 | |
| 4529 | /* Wait for the device to get ready */ |
Boris Brezillon | f1d4694 | 2018-09-06 14:05:29 +0200 | [diff] [blame] | 4530 | panic_nand_wait(chip, 400); |
Brent Taylor | 30863e38 | 2017-10-30 22:32:45 -0500 | [diff] [blame] | 4531 | |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 4532 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 4533 | ops.len = len; |
| 4534 | ops.datbuf = (uint8_t *)buf; |
Huang Shijie | 11041ae6 | 2012-07-03 16:44:14 +0800 | [diff] [blame] | 4535 | ops.mode = MTD_OPS_PLACE_OOB; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4536 | |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 4537 | ret = nand_do_write_ops(mtd, to, &ops); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4538 | |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 4539 | *retlen = ops.retlen; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 4540 | return ret; |
| 4541 | } |
| 4542 | |
| 4543 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4544 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4545 | * @mtd: MTD device structure |
| 4546 | * @to: offset to write to |
| 4547 | * @ops: oob operation description structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4548 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4549 | * NAND write out-of-band. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4550 | */ |
| 4551 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 4552 | struct mtd_oob_ops *ops) |
| 4553 | { |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 4554 | int chipnr, page, status, len; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4555 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4556 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4557 | pr_debug("%s: to = 0x%08x, len = %i\n", |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 4558 | __func__, (unsigned int)to, (int)ops->ooblen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4559 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 4560 | len = mtd_oobavail(mtd, ops); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 4561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4562 | /* Do not allow write past end of page */ |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 4563 | if ((ops->ooboffs + ops->ooblen) > len) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4564 | pr_debug("%s: attempt to write past end of page\n", |
| 4565 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | return -EINVAL; |
| 4567 | } |
| 4568 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 4569 | chipnr = (int)(to >> chip->chip_shift); |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 4570 | |
| 4571 | /* |
| 4572 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one |
| 4573 | * of my DiskOnChip 2000 test units) will clear the whole data page too |
| 4574 | * if we don't do this. I have no clue why, but I seem to have 'fixed' |
| 4575 | * it in the doc2000 driver in August 1999. dwmw2. |
| 4576 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4577 | nand_reset(chip, chipnr); |
| 4578 | |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4579 | chip->select_chip(chip, chipnr); |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4580 | |
| 4581 | /* Shift to get page */ |
| 4582 | page = (int)(to >> chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4583 | |
| 4584 | /* Check, if it is write protected */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 4585 | if (nand_check_wp(mtd)) { |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4586 | chip->select_chip(chip, -1); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4587 | return -EROFS; |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 4588 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | /* Invalidate the page cache, if we write to the cached page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4591 | if (page == chip->pagebuf) |
| 4592 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4593 | |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 4594 | nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops); |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 4595 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 4596 | if (ops->mode == MTD_OPS_RAW) |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4597 | status = chip->ecc.write_oob_raw(chip, page & chip->pagemask); |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 4598 | else |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame] | 4599 | status = chip->ecc.write_oob(chip, page & chip->pagemask); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4600 | |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4601 | chip->select_chip(chip, -1); |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 4602 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 4603 | if (status) |
| 4604 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4605 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 4606 | ops->oobretlen = ops->ooblen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4607 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 4608 | return 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4609 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4610 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4611 | /** |
| 4612 | * 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] | 4613 | * @mtd: MTD device structure |
| 4614 | * @to: offset to write to |
| 4615 | * @ops: oob operation description structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4616 | */ |
| 4617 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, |
| 4618 | struct mtd_oob_ops *ops) |
| 4619 | { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4620 | int ret = -ENOTSUPP; |
| 4621 | |
| 4622 | ops->retlen = 0; |
| 4623 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 4624 | nand_get_device(mtd, FL_WRITING); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4625 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 4626 | switch (ops->mode) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 4627 | case MTD_OPS_PLACE_OOB: |
| 4628 | case MTD_OPS_AUTO_OOB: |
| 4629 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4630 | break; |
| 4631 | |
| 4632 | default: |
| 4633 | goto out; |
| 4634 | } |
| 4635 | |
| 4636 | if (!ops->datbuf) |
| 4637 | ret = nand_do_write_oob(mtd, to, ops); |
| 4638 | else |
| 4639 | ret = nand_do_write_ops(mtd, to, ops); |
| 4640 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4641 | out: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 4642 | nand_release_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4643 | return ret; |
| 4644 | } |
| 4645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4646 | /** |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4647 | * single_erase - [GENERIC] NAND standard block erase command function |
Boris Brezillon | a2098a9 | 2018-09-06 14:05:30 +0200 | [diff] [blame] | 4648 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4649 | * @page: the page address of the block which will be erased |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4650 | * |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4651 | * Standard erase command for NAND chips. Returns NAND status. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4652 | */ |
Boris Brezillon | a2098a9 | 2018-09-06 14:05:30 +0200 | [diff] [blame] | 4653 | static int single_erase(struct nand_chip *chip, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4654 | { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4655 | unsigned int eraseblock; |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4657 | /* Send commands to erase a block */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4658 | eraseblock = page >> (chip->phys_erase_shift - chip->page_shift); |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4659 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 4660 | return nand_erase_op(chip, eraseblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4661 | } |
| 4662 | |
| 4663 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4664 | * nand_erase - [MTD Interface] erase block(s) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4665 | * @mtd: MTD device structure |
| 4666 | * @instr: erase instruction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4667 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4668 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4669 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4670 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4671 | { |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 4672 | return nand_erase_nand(mtd_to_nand(mtd), instr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4673 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4674 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4675 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 4676 | * nand_erase_nand - [INTERN] erase block(s) |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 4677 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4678 | * @instr: erase instruction |
| 4679 | * @allowbbt: allow erasing the bbt area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4680 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4681 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4682 | */ |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 4683 | int nand_erase_nand(struct nand_chip *chip, struct erase_info *instr, |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4684 | int allowbbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4685 | { |
Boris Brezillon | e4cdf9c | 2018-09-06 14:05:35 +0200 | [diff] [blame] | 4686 | struct mtd_info *mtd = nand_to_mtd(chip); |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4687 | int page, status, pages_per_block, ret, chipnr; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4688 | loff_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4690 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
| 4691 | __func__, (unsigned long long)instr->addr, |
| 4692 | (unsigned long long)instr->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4693 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 4694 | if (check_offs_len(mtd, instr->addr, instr->len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4695 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4697 | /* Grab the lock and see if the device is available */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 4698 | nand_get_device(mtd, FL_ERASING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4699 | |
| 4700 | /* Shift to get first page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4701 | page = (int)(instr->addr >> chip->page_shift); |
| 4702 | chipnr = (int)(instr->addr >> chip->chip_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4703 | |
| 4704 | /* Calculate pages in each block */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4705 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4706 | |
| 4707 | /* Select the NAND device */ |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4708 | chip->select_chip(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4710 | /* Check, if it is write protected */ |
| 4711 | if (nand_check_wp(mtd)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4712 | pr_debug("%s: device is write protected!\n", |
| 4713 | __func__); |
Boris Brezillon | e7bfb3f | 2018-02-12 22:03:11 +0100 | [diff] [blame] | 4714 | ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4715 | goto erase_exit; |
| 4716 | } |
| 4717 | |
| 4718 | /* Loop through the pages */ |
| 4719 | len = instr->len; |
| 4720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4721 | while (len) { |
Wolfram Sang | 12183a2 | 2011-12-21 23:01:20 +0100 | [diff] [blame] | 4722 | /* 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] | 4723 | if (nand_block_checkbad(mtd, ((loff_t) page) << |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4724 | chip->page_shift, allowbbt)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 4725 | pr_warn("%s: attempt to erase a bad block at page 0x%08x\n", |
| 4726 | __func__, page); |
Boris Brezillon | e7bfb3f | 2018-02-12 22:03:11 +0100 | [diff] [blame] | 4727 | ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4728 | goto erase_exit; |
| 4729 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4730 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4731 | /* |
| 4732 | * Invalidate the page cache, if we erase the block which |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4733 | * contains the current cached page. |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4734 | */ |
| 4735 | if (page <= chip->pagebuf && chip->pagebuf < |
| 4736 | (page + pages_per_block)) |
| 4737 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4738 | |
Boris Brezillon | f9ebd1b | 2018-09-07 00:38:39 +0200 | [diff] [blame] | 4739 | if (chip->legacy.erase) |
| 4740 | status = chip->legacy.erase(chip, |
| 4741 | page & chip->pagemask); |
| 4742 | else |
| 4743 | status = single_erase(chip, page & chip->pagemask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4744 | |
| 4745 | /* See if block erase succeeded */ |
Miquel Raynal | eb94555 | 2017-11-30 18:01:28 +0100 | [diff] [blame] | 4746 | if (status) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4747 | pr_debug("%s: failed erase, page 0x%08x\n", |
| 4748 | __func__, page); |
Boris Brezillon | e7bfb3f | 2018-02-12 22:03:11 +0100 | [diff] [blame] | 4749 | ret = -EIO; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4750 | instr->fail_addr = |
| 4751 | ((loff_t)page << chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4752 | goto erase_exit; |
| 4753 | } |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 4754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4755 | /* Increment page address and decrement length */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 4756 | len -= (1ULL << chip->phys_erase_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4757 | page += pages_per_block; |
| 4758 | |
| 4759 | /* Check, if we cross a chip boundary */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4760 | if (len && !(page & chip->pagemask)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4761 | chipnr++; |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4762 | chip->select_chip(chip, -1); |
| 4763 | chip->select_chip(chip, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4764 | } |
| 4765 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4766 | |
Boris Brezillon | e7bfb3f | 2018-02-12 22:03:11 +0100 | [diff] [blame] | 4767 | ret = 0; |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4768 | erase_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4769 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4770 | /* Deselect and wake up anyone waiting on the device */ |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4771 | chip->select_chip(chip, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4772 | nand_release_device(mtd); |
| 4773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4774 | /* Return more or less happy */ |
| 4775 | return ret; |
| 4776 | } |
| 4777 | |
| 4778 | /** |
| 4779 | * nand_sync - [MTD Interface] sync |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4780 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4781 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4782 | * Sync is actually a wait for chip ready function. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4783 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4784 | static void nand_sync(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4785 | { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 4786 | pr_debug("%s: called\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4787 | |
| 4788 | /* Grab the lock and see if the device is available */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 4789 | nand_get_device(mtd, FL_SYNCING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4790 | /* Release it and go back */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4791 | nand_release_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4792 | } |
| 4793 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4794 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4795 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4796 | * @mtd: MTD device structure |
| 4797 | * @offs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4798 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4799 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4800 | { |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4801 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 4802 | int chipnr = (int)(offs >> chip->chip_shift); |
| 4803 | int ret; |
| 4804 | |
| 4805 | /* Select the NAND device */ |
| 4806 | nand_get_device(mtd, FL_READING); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4807 | chip->select_chip(chip, chipnr); |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4808 | |
| 4809 | ret = nand_block_checkbad(mtd, offs, 0); |
| 4810 | |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 4811 | chip->select_chip(chip, -1); |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 4812 | nand_release_device(mtd); |
| 4813 | |
| 4814 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4815 | } |
| 4816 | |
| 4817 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4818 | * 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] | 4819 | * @mtd: MTD device structure |
| 4820 | * @ofs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4821 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4822 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4823 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4824 | int ret; |
| 4825 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 4826 | ret = nand_block_isbad(mtd, ofs); |
| 4827 | if (ret) { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4828 | /* If it was bad already, return success and do nothing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4829 | if (ret > 0) |
| 4830 | return 0; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4831 | return ret; |
| 4832 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4833 | |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 4834 | return nand_block_markbad_lowlevel(mtd, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4835 | } |
| 4836 | |
| 4837 | /** |
Zach Brown | 5671842 | 2017-01-10 13:30:20 -0600 | [diff] [blame] | 4838 | * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd |
| 4839 | * @mtd: MTD device structure |
| 4840 | * @ofs: offset relative to mtd start |
| 4841 | * @len: length of mtd |
| 4842 | */ |
| 4843 | static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len) |
| 4844 | { |
| 4845 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 4846 | u32 part_start_block; |
| 4847 | u32 part_end_block; |
| 4848 | u32 part_start_die; |
| 4849 | u32 part_end_die; |
| 4850 | |
| 4851 | /* |
| 4852 | * max_bb_per_die and blocks_per_die used to determine |
| 4853 | * the maximum bad block count. |
| 4854 | */ |
| 4855 | if (!chip->max_bb_per_die || !chip->blocks_per_die) |
| 4856 | return -ENOTSUPP; |
| 4857 | |
| 4858 | /* Get the start and end of the partition in erase blocks. */ |
| 4859 | part_start_block = mtd_div_by_eb(ofs, mtd); |
| 4860 | part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1; |
| 4861 | |
| 4862 | /* Get the start and end LUNs of the partition. */ |
| 4863 | part_start_die = part_start_block / chip->blocks_per_die; |
| 4864 | part_end_die = part_end_block / chip->blocks_per_die; |
| 4865 | |
| 4866 | /* |
| 4867 | * Look up the bad blocks per unit and multiply by the number of units |
| 4868 | * that the partition spans. |
| 4869 | */ |
| 4870 | return chip->max_bb_per_die * (part_end_die - part_start_die + 1); |
| 4871 | } |
| 4872 | |
| 4873 | /** |
Miquel Raynal | b958758 | 2018-03-19 14:47:19 +0100 | [diff] [blame] | 4874 | * nand_get_set_features_notsupp - set/get features stub returning -ENOTSUPP |
Boris Brezillon | 4a78cc6 | 2017-05-26 17:10:15 +0200 | [diff] [blame] | 4875 | * @chip: nand chip info structure |
| 4876 | * @addr: feature address. |
| 4877 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 4878 | * |
| 4879 | * Should be used by NAND controller drivers that do not support the SET/GET |
| 4880 | * FEATURES operations. |
| 4881 | */ |
Boris Brezillon | aa36ff2 | 2018-09-06 14:05:31 +0200 | [diff] [blame] | 4882 | int nand_get_set_features_notsupp(struct nand_chip *chip, int addr, |
| 4883 | u8 *subfeature_param) |
Boris Brezillon | 4a78cc6 | 2017-05-26 17:10:15 +0200 | [diff] [blame] | 4884 | { |
| 4885 | return -ENOTSUPP; |
| 4886 | } |
Miquel Raynal | b958758 | 2018-03-19 14:47:19 +0100 | [diff] [blame] | 4887 | EXPORT_SYMBOL(nand_get_set_features_notsupp); |
Boris Brezillon | 4a78cc6 | 2017-05-26 17:10:15 +0200 | [diff] [blame] | 4888 | |
| 4889 | /** |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4890 | * nand_suspend - [MTD Interface] Suspend the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4891 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4892 | */ |
| 4893 | static int nand_suspend(struct mtd_info *mtd) |
| 4894 | { |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 4895 | return nand_get_device(mtd, FL_PM_SUSPENDED); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4896 | } |
| 4897 | |
| 4898 | /** |
| 4899 | * nand_resume - [MTD Interface] Resume the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4900 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4901 | */ |
| 4902 | static void nand_resume(struct mtd_info *mtd) |
| 4903 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4904 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4905 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4906 | if (chip->state == FL_PM_SUSPENDED) |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4907 | nand_release_device(mtd); |
| 4908 | else |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 4909 | pr_err("%s called for a chip which is not in suspended state\n", |
| 4910 | __func__); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 4911 | } |
| 4912 | |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 4913 | /** |
| 4914 | * nand_shutdown - [MTD Interface] Finish the current NAND operation and |
| 4915 | * prevent further operations |
| 4916 | * @mtd: MTD device structure |
| 4917 | */ |
| 4918 | static void nand_shutdown(struct mtd_info *mtd) |
| 4919 | { |
Brian Norris | 9ca641b | 2015-11-09 16:37:28 -0800 | [diff] [blame] | 4920 | nand_get_device(mtd, FL_PM_SUSPENDED); |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 4921 | } |
| 4922 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4923 | /* Set default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4924 | static void nand_set_defaults(struct nand_chip *chip) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4925 | { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4926 | unsigned int busw = chip->options & NAND_BUSWIDTH_16; |
| 4927 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4928 | /* check for proper chip_delay setup, set 20us if not */ |
Boris Brezillon | 3cece3a | 2018-09-07 00:38:41 +0200 | [diff] [blame] | 4929 | if (!chip->legacy.chip_delay) |
| 4930 | chip->legacy.chip_delay = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4931 | |
| 4932 | /* check, if a user supplied command function given */ |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 4933 | if (!chip->legacy.cmdfunc && !chip->exec_op) |
| 4934 | chip->legacy.cmdfunc = nand_command; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4935 | |
| 4936 | /* check, if a user supplied wait function given */ |
Boris Brezillon | 8395b75 | 2018-09-07 00:38:37 +0200 | [diff] [blame] | 4937 | if (chip->legacy.waitfunc == NULL) |
| 4938 | chip->legacy.waitfunc = nand_wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4939 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4940 | if (!chip->select_chip) |
| 4941 | chip->select_chip = nand_select_chip; |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 4942 | |
| 4943 | /* If called twice, pointers that depend on busw may need to be reset */ |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 4944 | if (!chip->legacy.read_byte || chip->legacy.read_byte == nand_read_byte) |
| 4945 | chip->legacy.read_byte = busw ? nand_read_byte16 : nand_read_byte; |
Boris Brezillon | 716bbba | 2018-09-07 00:38:35 +0200 | [diff] [blame] | 4946 | if (!chip->legacy.write_buf || chip->legacy.write_buf == nand_write_buf) |
| 4947 | chip->legacy.write_buf = busw ? nand_write_buf16 : nand_write_buf; |
| 4948 | if (!chip->legacy.write_byte || chip->legacy.write_byte == nand_write_byte) |
| 4949 | chip->legacy.write_byte = busw ? nand_write_byte16 : nand_write_byte; |
| 4950 | if (!chip->legacy.read_buf || chip->legacy.read_buf == nand_read_buf) |
| 4951 | chip->legacy.read_buf = busw ? nand_read_buf16 : nand_read_buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4952 | |
| 4953 | if (!chip->controller) { |
Miquel Raynal | 7da4513 | 2018-07-17 09:08:02 +0200 | [diff] [blame] | 4954 | chip->controller = &chip->dummy_controller; |
| 4955 | nand_controller_init(chip->controller); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 4956 | } |
| 4957 | |
Masahiro Yamada | 477544c | 2017-03-30 17:15:05 +0900 | [diff] [blame] | 4958 | if (!chip->buf_align) |
| 4959 | chip->buf_align = 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4960 | } |
| 4961 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4962 | /* Sanitize ONFI strings so we can safely print them */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4963 | static void sanitize_string(uint8_t *s, size_t len) |
| 4964 | { |
| 4965 | ssize_t i; |
| 4966 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4967 | /* Null terminate */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4968 | s[len - 1] = 0; |
| 4969 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4970 | /* Remove non printable chars */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4971 | for (i = 0; i < len - 1; i++) { |
| 4972 | if (s[i] < ' ' || s[i] > 127) |
| 4973 | s[i] = '?'; |
| 4974 | } |
| 4975 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4976 | /* Remove trailing spaces */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4977 | strim(s); |
| 4978 | } |
| 4979 | |
| 4980 | static u16 onfi_crc16(u16 crc, u8 const *p, size_t len) |
| 4981 | { |
| 4982 | int i; |
| 4983 | while (len--) { |
| 4984 | crc ^= *p++ << 8; |
| 4985 | for (i = 0; i < 8; i++) |
| 4986 | crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0); |
| 4987 | } |
| 4988 | |
| 4989 | return crc; |
| 4990 | } |
| 4991 | |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 4992 | /* Parse the Extended Parameter Page. */ |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4993 | static int nand_flash_detect_ext_param_page(struct nand_chip *chip, |
| 4994 | struct nand_onfi_params *p) |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 4995 | { |
| 4996 | struct onfi_ext_param_page *ep; |
| 4997 | struct onfi_ext_section *s; |
| 4998 | struct onfi_ext_ecc_info *ecc; |
| 4999 | uint8_t *cursor; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5000 | int ret; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 5001 | int len; |
| 5002 | int i; |
| 5003 | |
| 5004 | len = le16_to_cpu(p->ext_param_page_length) * 16; |
| 5005 | ep = kmalloc(len, GFP_KERNEL); |
Brian Norris | 5cb1327 | 2013-09-16 17:59:20 -0700 | [diff] [blame] | 5006 | if (!ep) |
| 5007 | return -ENOMEM; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 5008 | |
| 5009 | /* Send our own NAND_CMD_PARAM. */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5010 | ret = nand_read_param_page_op(chip, 0, NULL, 0); |
| 5011 | if (ret) |
| 5012 | goto ext_out; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 5013 | |
| 5014 | /* Use the Change Read Column command to skip the ONFI param pages. */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5015 | ret = nand_change_read_column_op(chip, |
| 5016 | sizeof(*p) * p->num_of_param_pages, |
| 5017 | ep, len, true); |
| 5018 | if (ret) |
| 5019 | goto ext_out; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 5020 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5021 | ret = -EINVAL; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 5022 | if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2) |
| 5023 | != le16_to_cpu(ep->crc))) { |
| 5024 | pr_debug("fail in the CRC.\n"); |
| 5025 | goto ext_out; |
| 5026 | } |
| 5027 | |
| 5028 | /* |
| 5029 | * Check the signature. |
| 5030 | * Do not strictly follow the ONFI spec, maybe changed in future. |
| 5031 | */ |
| 5032 | if (strncmp(ep->sig, "EPPS", 4)) { |
| 5033 | pr_debug("The signature is invalid.\n"); |
| 5034 | goto ext_out; |
| 5035 | } |
| 5036 | |
| 5037 | /* find the ECC section. */ |
| 5038 | cursor = (uint8_t *)(ep + 1); |
| 5039 | for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) { |
| 5040 | s = ep->sections + i; |
| 5041 | if (s->type == ONFI_SECTION_TYPE_2) |
| 5042 | break; |
| 5043 | cursor += s->length * 16; |
| 5044 | } |
| 5045 | if (i == ONFI_EXT_SECTION_MAX) { |
| 5046 | pr_debug("We can not find the ECC section.\n"); |
| 5047 | goto ext_out; |
| 5048 | } |
| 5049 | |
| 5050 | /* get the info we want. */ |
| 5051 | ecc = (struct onfi_ext_ecc_info *)cursor; |
| 5052 | |
Brian Norris | 4ae7d22 | 2013-09-16 18:20:21 -0700 | [diff] [blame] | 5053 | if (!ecc->codeword_size) { |
| 5054 | pr_debug("Invalid codeword size\n"); |
| 5055 | goto ext_out; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 5056 | } |
| 5057 | |
Brian Norris | 4ae7d22 | 2013-09-16 18:20:21 -0700 | [diff] [blame] | 5058 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 5059 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
Brian Norris | 5cb1327 | 2013-09-16 17:59:20 -0700 | [diff] [blame] | 5060 | ret = 0; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 5061 | |
| 5062 | ext_out: |
| 5063 | kfree(ep); |
| 5064 | return ret; |
| 5065 | } |
| 5066 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 5067 | /* |
Wan, Jane (Nokia - US/Sunnyvale) | 39138c1 | 2018-05-13 04:30:02 +0000 | [diff] [blame] | 5068 | * Recover data with bit-wise majority |
| 5069 | */ |
| 5070 | static void nand_bit_wise_majority(const void **srcbufs, |
| 5071 | unsigned int nsrcbufs, |
| 5072 | void *dstbuf, |
| 5073 | unsigned int bufsize) |
| 5074 | { |
| 5075 | int i, j, k; |
| 5076 | |
| 5077 | for (i = 0; i < bufsize; i++) { |
| 5078 | u8 val = 0; |
| 5079 | |
| 5080 | for (j = 0; j < 8; j++) { |
| 5081 | unsigned int cnt = 0; |
| 5082 | |
| 5083 | for (k = 0; k < nsrcbufs; k++) { |
| 5084 | const u8 *srcbuf = srcbufs[k]; |
| 5085 | |
| 5086 | if (srcbuf[i] & BIT(j)) |
| 5087 | cnt++; |
| 5088 | } |
| 5089 | |
| 5090 | if (cnt > nsrcbufs / 2) |
| 5091 | val |= BIT(j); |
| 5092 | } |
| 5093 | |
| 5094 | ((u8 *)dstbuf)[i] = val; |
| 5095 | } |
| 5096 | } |
| 5097 | |
| 5098 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5099 | * 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] | 5100 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5101 | static int nand_flash_detect_onfi(struct nand_chip *chip) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5102 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 5103 | struct mtd_info *mtd = nand_to_mtd(chip); |
Miquel Raynal | bd0b643 | 2018-03-19 14:47:31 +0100 | [diff] [blame] | 5104 | struct nand_onfi_params *p; |
Miquel Raynal | 3d3fe3c | 2018-07-25 15:31:52 +0200 | [diff] [blame] | 5105 | struct onfi_params *onfi; |
| 5106 | int onfi_version = 0; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5107 | char id[4]; |
| 5108 | int i, ret, val; |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5109 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 5110 | /* Try ONFI for unknown chip or LP */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5111 | ret = nand_readid_op(chip, 0x20, id, sizeof(id)); |
| 5112 | if (ret || strncmp(id, "ONFI", 4)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5113 | return 0; |
| 5114 | |
Miquel Raynal | bd0b643 | 2018-03-19 14:47:31 +0100 | [diff] [blame] | 5115 | /* ONFI chip: allocate a buffer to hold its parameter page */ |
Wan, Jane (Nokia - US/Sunnyvale) | 39138c1 | 2018-05-13 04:30:02 +0000 | [diff] [blame] | 5116 | p = kzalloc((sizeof(*p) * 3), GFP_KERNEL); |
Miquel Raynal | bd0b643 | 2018-03-19 14:47:31 +0100 | [diff] [blame] | 5117 | if (!p) |
| 5118 | return -ENOMEM; |
| 5119 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5120 | ret = nand_read_param_page_op(chip, 0, NULL, 0); |
Miquel Raynal | bd0b643 | 2018-03-19 14:47:31 +0100 | [diff] [blame] | 5121 | if (ret) { |
| 5122 | ret = 0; |
| 5123 | goto free_onfi_param_page; |
| 5124 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5125 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5126 | for (i = 0; i < 3; i++) { |
Wan, Jane (Nokia - US/Sunnyvale) | 39138c1 | 2018-05-13 04:30:02 +0000 | [diff] [blame] | 5127 | ret = nand_read_data_op(chip, &p[i], sizeof(*p), true); |
Miquel Raynal | bd0b643 | 2018-03-19 14:47:31 +0100 | [diff] [blame] | 5128 | if (ret) { |
| 5129 | ret = 0; |
| 5130 | goto free_onfi_param_page; |
| 5131 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5132 | |
Wan, Jane (Nokia - US/Sunnyvale) | 39138c1 | 2018-05-13 04:30:02 +0000 | [diff] [blame] | 5133 | if (onfi_crc16(ONFI_CRC_BASE, (u8 *)&p[i], 254) == |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5134 | le16_to_cpu(p->crc)) { |
Wan, Jane (Nokia - US/Sunnyvale) | 39138c1 | 2018-05-13 04:30:02 +0000 | [diff] [blame] | 5135 | if (i) |
| 5136 | memcpy(p, &p[i], sizeof(*p)); |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5137 | break; |
| 5138 | } |
| 5139 | } |
| 5140 | |
Brian Norris | c7f23a7 | 2013-08-13 10:51:55 -0700 | [diff] [blame] | 5141 | if (i == 3) { |
Wan, Jane (Nokia - US/Sunnyvale) | 39138c1 | 2018-05-13 04:30:02 +0000 | [diff] [blame] | 5142 | const void *srcbufs[3] = {p, p + 1, p + 2}; |
| 5143 | |
| 5144 | pr_warn("Could not find a valid ONFI parameter page, trying bit-wise majority to recover it\n"); |
| 5145 | nand_bit_wise_majority(srcbufs, ARRAY_SIZE(srcbufs), p, |
| 5146 | sizeof(*p)); |
| 5147 | |
| 5148 | if (onfi_crc16(ONFI_CRC_BASE, (u8 *)p, 254) != |
| 5149 | le16_to_cpu(p->crc)) { |
| 5150 | pr_err("ONFI parameter recovery failed, aborting\n"); |
| 5151 | goto free_onfi_param_page; |
| 5152 | } |
Brian Norris | c7f23a7 | 2013-08-13 10:51:55 -0700 | [diff] [blame] | 5153 | } |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5154 | |
Chris Packham | 00ce4e0 | 2018-06-25 10:44:44 +1200 | [diff] [blame] | 5155 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
| 5156 | chip->manufacturer.desc->ops->fixup_onfi_param_page) |
| 5157 | chip->manufacturer.desc->ops->fixup_onfi_param_page(chip, p); |
| 5158 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5159 | /* Check version */ |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5160 | val = le16_to_cpu(p->revision); |
Chris Packham | 872b71f | 2018-06-25 10:44:45 +1200 | [diff] [blame] | 5161 | if (val & ONFI_VERSION_2_3) |
Miquel Raynal | 3d3fe3c | 2018-07-25 15:31:52 +0200 | [diff] [blame] | 5162 | onfi_version = 23; |
Chris Packham | 872b71f | 2018-06-25 10:44:45 +1200 | [diff] [blame] | 5163 | else if (val & ONFI_VERSION_2_2) |
Miquel Raynal | 3d3fe3c | 2018-07-25 15:31:52 +0200 | [diff] [blame] | 5164 | onfi_version = 22; |
Chris Packham | 872b71f | 2018-06-25 10:44:45 +1200 | [diff] [blame] | 5165 | else if (val & ONFI_VERSION_2_1) |
Miquel Raynal | 3d3fe3c | 2018-07-25 15:31:52 +0200 | [diff] [blame] | 5166 | onfi_version = 21; |
Chris Packham | 872b71f | 2018-06-25 10:44:45 +1200 | [diff] [blame] | 5167 | else if (val & ONFI_VERSION_2_0) |
Miquel Raynal | 3d3fe3c | 2018-07-25 15:31:52 +0200 | [diff] [blame] | 5168 | onfi_version = 20; |
Chris Packham | 872b71f | 2018-06-25 10:44:45 +1200 | [diff] [blame] | 5169 | else if (val & ONFI_VERSION_1_0) |
Miquel Raynal | 3d3fe3c | 2018-07-25 15:31:52 +0200 | [diff] [blame] | 5170 | onfi_version = 10; |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 5171 | |
Miquel Raynal | 3d3fe3c | 2018-07-25 15:31:52 +0200 | [diff] [blame] | 5172 | if (!onfi_version) { |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 5173 | pr_info("unsupported ONFI version: %d\n", val); |
Miquel Raynal | bd0b643 | 2018-03-19 14:47:31 +0100 | [diff] [blame] | 5174 | goto free_onfi_param_page; |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 5175 | } |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5176 | |
| 5177 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 5178 | sanitize_string(p->model, sizeof(p->model)); |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5179 | chip->parameters.model = kstrdup(p->model, GFP_KERNEL); |
| 5180 | if (!chip->parameters.model) { |
| 5181 | ret = -ENOMEM; |
| 5182 | goto free_onfi_param_page; |
| 5183 | } |
Brian Norris | 4355b70 | 2013-08-27 18:45:10 -0700 | [diff] [blame] | 5184 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5185 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
Brian Norris | 4355b70 | 2013-08-27 18:45:10 -0700 | [diff] [blame] | 5186 | |
| 5187 | /* |
| 5188 | * pages_per_block and blocks_per_lun may not be a power-of-2 size |
| 5189 | * (don't ask me who thought of this...). MTD assumes that these |
| 5190 | * dimensions will be power-of-2, so just truncate the remaining area. |
| 5191 | */ |
| 5192 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 5193 | mtd->erasesize *= mtd->writesize; |
| 5194 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5195 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
Brian Norris | 4355b70 | 2013-08-27 18:45:10 -0700 | [diff] [blame] | 5196 | |
| 5197 | /* See erasesize comment */ |
| 5198 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
Matthieu CASTET | 6379575 | 2012-03-19 15:35:25 +0100 | [diff] [blame] | 5199 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
Huang Shijie | 13fbd17 | 2013-09-25 14:58:13 +0800 | [diff] [blame] | 5200 | chip->bits_per_cell = p->bits_per_cell; |
Huang Shijie | e2985fc | 2013-05-17 11:17:30 +0800 | [diff] [blame] | 5201 | |
Zach Brown | 34da5f5 | 2017-01-10 13:30:21 -0600 | [diff] [blame] | 5202 | chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun); |
| 5203 | chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun); |
| 5204 | |
Miquel Raynal | a97421c | 2018-03-19 14:47:27 +0100 | [diff] [blame] | 5205 | if (le16_to_cpu(p->features) & ONFI_FEATURE_16_BIT_BUS) |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5206 | chip->options |= NAND_BUSWIDTH_16; |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5207 | |
Huang Shijie | 10c86ba | 2013-05-17 11:17:26 +0800 | [diff] [blame] | 5208 | if (p->ecc_bits != 0xff) { |
| 5209 | chip->ecc_strength_ds = p->ecc_bits; |
| 5210 | chip->ecc_step_ds = 512; |
Miquel Raynal | 3d3fe3c | 2018-07-25 15:31:52 +0200 | [diff] [blame] | 5211 | } else if (onfi_version >= 21 && |
Miquel Raynal | a97421c | 2018-03-19 14:47:27 +0100 | [diff] [blame] | 5212 | (le16_to_cpu(p->features) & ONFI_FEATURE_EXT_PARAM_PAGE)) { |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 5213 | |
| 5214 | /* |
| 5215 | * The nand_flash_detect_ext_param_page() uses the |
| 5216 | * Change Read Column command which maybe not supported |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 5217 | * by the chip->legacy.cmdfunc. So try to update the |
| 5218 | * chip->legacy.cmdfunc now. We do not replace user supplied |
| 5219 | * command function. |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 5220 | */ |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 5221 | if (mtd->writesize > 512 && |
| 5222 | chip->legacy.cmdfunc == nand_command) |
| 5223 | chip->legacy.cmdfunc = nand_command_lp; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 5224 | |
| 5225 | /* The Extended Parameter Page is supported since ONFI 2.1. */ |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 5226 | if (nand_flash_detect_ext_param_page(chip, p)) |
Brian Norris | c7f23a7 | 2013-08-13 10:51:55 -0700 | [diff] [blame] | 5227 | pr_warn("Failed to detect ONFI extended param page\n"); |
| 5228 | } else { |
| 5229 | pr_warn("Could not retrieve ONFI ECC requirements\n"); |
Huang Shijie | 10c86ba | 2013-05-17 11:17:26 +0800 | [diff] [blame] | 5230 | } |
| 5231 | |
Miquel Raynal | f4531b2 | 2018-03-19 14:47:26 +0100 | [diff] [blame] | 5232 | /* Save some parameters from the parameter page for future use */ |
Miquel Raynal | 789157e | 2018-03-19 14:47:28 +0100 | [diff] [blame] | 5233 | if (le16_to_cpu(p->opt_cmd) & ONFI_OPT_CMD_SET_GET_FEATURES) { |
Miquel Raynal | f4531b2 | 2018-03-19 14:47:26 +0100 | [diff] [blame] | 5234 | chip->parameters.supports_set_get_features = true; |
Miquel Raynal | 789157e | 2018-03-19 14:47:28 +0100 | [diff] [blame] | 5235 | bitmap_set(chip->parameters.get_feature_list, |
| 5236 | ONFI_FEATURE_ADDR_TIMING_MODE, 1); |
| 5237 | bitmap_set(chip->parameters.set_feature_list, |
| 5238 | ONFI_FEATURE_ADDR_TIMING_MODE, 1); |
| 5239 | } |
Miquel Raynal | f4531b2 | 2018-03-19 14:47:26 +0100 | [diff] [blame] | 5240 | |
Miquel Raynal | 3d3fe3c | 2018-07-25 15:31:52 +0200 | [diff] [blame] | 5241 | onfi = kzalloc(sizeof(*onfi), GFP_KERNEL); |
| 5242 | if (!onfi) { |
| 5243 | ret = -ENOMEM; |
| 5244 | goto free_model; |
| 5245 | } |
| 5246 | |
| 5247 | onfi->version = onfi_version; |
| 5248 | onfi->tPROG = le16_to_cpu(p->t_prog); |
| 5249 | onfi->tBERS = le16_to_cpu(p->t_bers); |
| 5250 | onfi->tR = le16_to_cpu(p->t_r); |
| 5251 | onfi->tCCS = le16_to_cpu(p->t_ccs); |
| 5252 | onfi->async_timing_mode = le16_to_cpu(p->async_timing_mode); |
| 5253 | onfi->vendor_revision = le16_to_cpu(p->vendor_revision); |
| 5254 | memcpy(onfi->vendor, p->vendor, sizeof(p->vendor)); |
| 5255 | chip->parameters.onfi = onfi; |
| 5256 | |
| 5257 | /* Identification done, free the full ONFI parameter page and exit */ |
| 5258 | kfree(p); |
| 5259 | |
| 5260 | return 1; |
| 5261 | |
| 5262 | free_model: |
| 5263 | kfree(chip->parameters.model); |
Miquel Raynal | bd0b643 | 2018-03-19 14:47:31 +0100 | [diff] [blame] | 5264 | free_onfi_param_page: |
| 5265 | kfree(p); |
Miquel Raynal | 3d3fe3c | 2018-07-25 15:31:52 +0200 | [diff] [blame] | 5266 | |
Miquel Raynal | bd0b643 | 2018-03-19 14:47:31 +0100 | [diff] [blame] | 5267 | return ret; |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5268 | } |
| 5269 | |
| 5270 | /* |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5271 | * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise. |
| 5272 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5273 | static int nand_flash_detect_jedec(struct nand_chip *chip) |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5274 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 5275 | struct mtd_info *mtd = nand_to_mtd(chip); |
Miquel Raynal | 480139d | 2018-03-19 14:47:30 +0100 | [diff] [blame] | 5276 | struct nand_jedec_params *p; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5277 | struct jedec_ecc_info *ecc; |
Miquel Raynal | 480139d | 2018-03-19 14:47:30 +0100 | [diff] [blame] | 5278 | int jedec_version = 0; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5279 | char id[5]; |
| 5280 | int i, val, ret; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5281 | |
| 5282 | /* Try JEDEC for unknown chip or LP */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5283 | ret = nand_readid_op(chip, 0x40, id, sizeof(id)); |
| 5284 | if (ret || strncmp(id, "JEDEC", sizeof(id))) |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5285 | return 0; |
| 5286 | |
Miquel Raynal | 480139d | 2018-03-19 14:47:30 +0100 | [diff] [blame] | 5287 | /* JEDEC chip: allocate a buffer to hold its parameter page */ |
| 5288 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
| 5289 | if (!p) |
| 5290 | return -ENOMEM; |
| 5291 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5292 | ret = nand_read_param_page_op(chip, 0x40, NULL, 0); |
Miquel Raynal | 480139d | 2018-03-19 14:47:30 +0100 | [diff] [blame] | 5293 | if (ret) { |
| 5294 | ret = 0; |
| 5295 | goto free_jedec_param_page; |
| 5296 | } |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5297 | |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5298 | for (i = 0; i < 3; i++) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5299 | ret = nand_read_data_op(chip, p, sizeof(*p), true); |
Miquel Raynal | 480139d | 2018-03-19 14:47:30 +0100 | [diff] [blame] | 5300 | if (ret) { |
| 5301 | ret = 0; |
| 5302 | goto free_jedec_param_page; |
| 5303 | } |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5304 | |
| 5305 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) == |
| 5306 | le16_to_cpu(p->crc)) |
| 5307 | break; |
| 5308 | } |
| 5309 | |
| 5310 | if (i == 3) { |
| 5311 | pr_err("Could not find valid JEDEC parameter page; aborting\n"); |
Miquel Raynal | 480139d | 2018-03-19 14:47:30 +0100 | [diff] [blame] | 5312 | goto free_jedec_param_page; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5313 | } |
| 5314 | |
| 5315 | /* Check version */ |
| 5316 | val = le16_to_cpu(p->revision); |
| 5317 | if (val & (1 << 2)) |
Miquel Raynal | 480139d | 2018-03-19 14:47:30 +0100 | [diff] [blame] | 5318 | jedec_version = 10; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5319 | else if (val & (1 << 1)) |
Miquel Raynal | 480139d | 2018-03-19 14:47:30 +0100 | [diff] [blame] | 5320 | jedec_version = 1; /* vendor specific version */ |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5321 | |
Miquel Raynal | 480139d | 2018-03-19 14:47:30 +0100 | [diff] [blame] | 5322 | if (!jedec_version) { |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5323 | pr_info("unsupported JEDEC version: %d\n", val); |
Miquel Raynal | 480139d | 2018-03-19 14:47:30 +0100 | [diff] [blame] | 5324 | goto free_jedec_param_page; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5325 | } |
| 5326 | |
| 5327 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 5328 | sanitize_string(p->model, sizeof(p->model)); |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5329 | chip->parameters.model = kstrdup(p->model, GFP_KERNEL); |
| 5330 | if (!chip->parameters.model) { |
| 5331 | ret = -ENOMEM; |
| 5332 | goto free_jedec_param_page; |
| 5333 | } |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5334 | |
| 5335 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
| 5336 | |
| 5337 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 5338 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 5339 | mtd->erasesize *= mtd->writesize; |
| 5340 | |
| 5341 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
| 5342 | |
| 5343 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 5344 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
| 5345 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
| 5346 | chip->bits_per_cell = p->bits_per_cell; |
| 5347 | |
Miquel Raynal | 480139d | 2018-03-19 14:47:30 +0100 | [diff] [blame] | 5348 | if (le16_to_cpu(p->features) & JEDEC_FEATURE_16_BIT_BUS) |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5349 | chip->options |= NAND_BUSWIDTH_16; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5350 | |
| 5351 | /* ECC info */ |
| 5352 | ecc = &p->ecc_info[0]; |
| 5353 | |
| 5354 | if (ecc->codeword_size >= 9) { |
| 5355 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 5356 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
| 5357 | } else { |
| 5358 | pr_warn("Invalid codeword size\n"); |
| 5359 | } |
| 5360 | |
Miquel Raynal | 480139d | 2018-03-19 14:47:30 +0100 | [diff] [blame] | 5361 | free_jedec_param_page: |
| 5362 | kfree(p); |
| 5363 | return ret; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5364 | } |
| 5365 | |
| 5366 | /* |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 5367 | * nand_id_has_period - Check if an ID string has a given wraparound period |
| 5368 | * @id_data: the ID string |
| 5369 | * @arrlen: the length of the @id_data array |
| 5370 | * @period: the period of repitition |
| 5371 | * |
| 5372 | * Check if an ID string is repeated within a given sequence of bytes at |
| 5373 | * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a |
Brian Norris | d4d4f1b | 2012-11-14 21:54:20 -0800 | [diff] [blame] | 5374 | * 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] | 5375 | * if the repetition has a period of @period; otherwise, returns zero. |
| 5376 | */ |
| 5377 | static int nand_id_has_period(u8 *id_data, int arrlen, int period) |
| 5378 | { |
| 5379 | int i, j; |
| 5380 | for (i = 0; i < period; i++) |
| 5381 | for (j = i + period; j < arrlen; j += period) |
| 5382 | if (id_data[i] != id_data[j]) |
| 5383 | return 0; |
| 5384 | return 1; |
| 5385 | } |
| 5386 | |
| 5387 | /* |
| 5388 | * nand_id_len - Get the length of an ID string returned by CMD_READID |
| 5389 | * @id_data: the ID string |
| 5390 | * @arrlen: the length of the @id_data array |
| 5391 | |
| 5392 | * Returns the length of the ID string, according to known wraparound/trailing |
| 5393 | * zero patterns. If no pattern exists, returns the length of the array. |
| 5394 | */ |
| 5395 | static int nand_id_len(u8 *id_data, int arrlen) |
| 5396 | { |
| 5397 | int last_nonzero, period; |
| 5398 | |
| 5399 | /* Find last non-zero byte */ |
| 5400 | for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--) |
| 5401 | if (id_data[last_nonzero]) |
| 5402 | break; |
| 5403 | |
| 5404 | /* All zeros */ |
| 5405 | if (last_nonzero < 0) |
| 5406 | return 0; |
| 5407 | |
| 5408 | /* Calculate wraparound period */ |
| 5409 | for (period = 1; period < arrlen; period++) |
| 5410 | if (nand_id_has_period(id_data, arrlen, period)) |
| 5411 | break; |
| 5412 | |
| 5413 | /* There's a repeated pattern */ |
| 5414 | if (period < arrlen) |
| 5415 | return period; |
| 5416 | |
| 5417 | /* There are trailing zeros */ |
| 5418 | if (last_nonzero < arrlen - 1) |
| 5419 | return last_nonzero + 1; |
| 5420 | |
| 5421 | /* No pattern detected */ |
| 5422 | return arrlen; |
| 5423 | } |
| 5424 | |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 5425 | /* Extract the bits of per cell from the 3rd byte of the extended ID */ |
| 5426 | static int nand_get_bits_per_cell(u8 cellinfo) |
| 5427 | { |
| 5428 | int bits; |
| 5429 | |
| 5430 | bits = cellinfo & NAND_CI_CELLTYPE_MSK; |
| 5431 | bits >>= NAND_CI_CELLTYPE_SHIFT; |
| 5432 | return bits + 1; |
| 5433 | } |
| 5434 | |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 5435 | /* |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 5436 | * Many new NAND share similar device ID codes, which represent the size of the |
| 5437 | * chip. The rest of the parameters must be decoded according to generic or |
| 5438 | * manufacturer-specific "extended ID" decoding patterns. |
| 5439 | */ |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5440 | void nand_decode_ext_id(struct nand_chip *chip) |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 5441 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 5442 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 9b2d61f | 2016-06-08 10:34:57 +0200 | [diff] [blame] | 5443 | int extid; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 5444 | u8 *id_data = chip->id.data; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 5445 | /* The 3rd id byte holds MLC / multichip data */ |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 5446 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 5447 | /* The 4th id byte is the important one */ |
| 5448 | extid = id_data[3]; |
| 5449 | |
Boris Brezillon | 01389b6 | 2016-06-08 10:30:18 +0200 | [diff] [blame] | 5450 | /* Calc pagesize */ |
| 5451 | mtd->writesize = 1024 << (extid & 0x03); |
| 5452 | extid >>= 2; |
| 5453 | /* Calc oobsize */ |
| 5454 | mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9); |
| 5455 | extid >>= 2; |
| 5456 | /* Calc blocksize. Blocksize is multiples of 64KiB */ |
| 5457 | mtd->erasesize = (64 * 1024) << (extid & 0x03); |
| 5458 | extid >>= 2; |
| 5459 | /* Get buswidth information */ |
| 5460 | if (extid & 0x1) |
| 5461 | chip->options |= NAND_BUSWIDTH_16; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 5462 | } |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5463 | EXPORT_SYMBOL_GPL(nand_decode_ext_id); |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 5464 | |
| 5465 | /* |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 5466 | * Old devices have chip data hardcoded in the device ID table. nand_decode_id |
| 5467 | * decodes a matching ID table entry and assigns the MTD size parameters for |
| 5468 | * the chip. |
| 5469 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5470 | 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] | 5471 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 5472 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 5473 | |
| 5474 | mtd->erasesize = type->erasesize; |
| 5475 | mtd->writesize = type->pagesize; |
| 5476 | mtd->oobsize = mtd->writesize / 32; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 5477 | |
Huang Shijie | 1c195e9 | 2013-09-25 14:58:12 +0800 | [diff] [blame] | 5478 | /* All legacy ID NAND are small-page, SLC */ |
| 5479 | chip->bits_per_cell = 1; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 5480 | } |
| 5481 | |
| 5482 | /* |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 5483 | * Set the bad block marker/indicator (BBM/BBI) patterns according to some |
| 5484 | * heuristic patterns using various detected parameters (e.g., manufacturer, |
| 5485 | * page size, cell-type information). |
| 5486 | */ |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 5487 | static void nand_decode_bbm_options(struct nand_chip *chip) |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 5488 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 5489 | struct mtd_info *mtd = nand_to_mtd(chip); |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 5490 | |
| 5491 | /* Set the bad block position */ |
| 5492 | if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16)) |
| 5493 | chip->badblockpos = NAND_LARGE_BADBLOCK_POS; |
| 5494 | else |
| 5495 | chip->badblockpos = NAND_SMALL_BADBLOCK_POS; |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 5496 | } |
| 5497 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 5498 | static inline bool is_full_id_nand(struct nand_flash_dev *type) |
| 5499 | { |
| 5500 | return type->id_len; |
| 5501 | } |
| 5502 | |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 5503 | static bool find_full_id_nand(struct nand_chip *chip, |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5504 | struct nand_flash_dev *type) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 5505 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 5506 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 5507 | u8 *id_data = chip->id.data; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 5508 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 5509 | if (!strncmp(type->id, id_data, type->id_len)) { |
| 5510 | mtd->writesize = type->pagesize; |
| 5511 | mtd->erasesize = type->erasesize; |
| 5512 | mtd->oobsize = type->oobsize; |
| 5513 | |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 5514 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 5515 | chip->chipsize = (uint64_t)type->chipsize << 20; |
| 5516 | chip->options |= type->options; |
Huang Shijie | 5721934 | 2013-05-17 11:17:32 +0800 | [diff] [blame] | 5517 | chip->ecc_strength_ds = NAND_ECC_STRENGTH(type); |
| 5518 | chip->ecc_step_ds = NAND_ECC_STEP(type); |
Boris BREZILLON | 57a94e2 | 2014-09-22 20:11:50 +0200 | [diff] [blame] | 5519 | chip->onfi_timing_mode_default = |
| 5520 | type->onfi_timing_mode_default; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 5521 | |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5522 | chip->parameters.model = kstrdup(type->name, GFP_KERNEL); |
| 5523 | if (!chip->parameters.model) |
| 5524 | return false; |
Cai Zhiyong | 092b6a1 | 2013-12-25 21:19:21 +0800 | [diff] [blame] | 5525 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 5526 | return true; |
| 5527 | } |
| 5528 | return false; |
| 5529 | } |
| 5530 | |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 5531 | /* |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5532 | * Manufacturer detection. Only used when the NAND is not ONFI or JEDEC |
| 5533 | * compliant and does not have a full-id or legacy-id entry in the nand_ids |
| 5534 | * table. |
| 5535 | */ |
| 5536 | static void nand_manufacturer_detect(struct nand_chip *chip) |
| 5537 | { |
| 5538 | /* |
| 5539 | * Try manufacturer detection if available and use |
| 5540 | * nand_decode_ext_id() otherwise. |
| 5541 | */ |
| 5542 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 5543 | chip->manufacturer.desc->ops->detect) { |
| 5544 | /* The 3rd id byte holds MLC / multichip data */ |
| 5545 | chip->bits_per_cell = nand_get_bits_per_cell(chip->id.data[2]); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5546 | chip->manufacturer.desc->ops->detect(chip); |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 5547 | } else { |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5548 | nand_decode_ext_id(chip); |
Lothar Waßmann | 69fc012 | 2017-08-29 12:17:12 +0200 | [diff] [blame] | 5549 | } |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5550 | } |
| 5551 | |
| 5552 | /* |
| 5553 | * Manufacturer initialization. This function is called for all NANDs including |
| 5554 | * ONFI and JEDEC compliant ones. |
| 5555 | * Manufacturer drivers should put all their specific initialization code in |
| 5556 | * their ->init() hook. |
| 5557 | */ |
| 5558 | static int nand_manufacturer_init(struct nand_chip *chip) |
| 5559 | { |
| 5560 | if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops || |
| 5561 | !chip->manufacturer.desc->ops->init) |
| 5562 | return 0; |
| 5563 | |
| 5564 | return chip->manufacturer.desc->ops->init(chip); |
| 5565 | } |
| 5566 | |
| 5567 | /* |
| 5568 | * Manufacturer cleanup. This function is called for all NANDs including |
| 5569 | * ONFI and JEDEC compliant ones. |
| 5570 | * Manufacturer drivers should put all their specific cleanup code in their |
| 5571 | * ->cleanup() hook. |
| 5572 | */ |
| 5573 | static void nand_manufacturer_cleanup(struct nand_chip *chip) |
| 5574 | { |
| 5575 | /* Release manufacturer private data */ |
| 5576 | if (chip->manufacturer.desc && chip->manufacturer.desc->ops && |
| 5577 | chip->manufacturer.desc->ops->cleanup) |
| 5578 | chip->manufacturer.desc->ops->cleanup(chip); |
| 5579 | } |
| 5580 | |
| 5581 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5582 | * 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] | 5583 | */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame] | 5584 | 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] | 5585 | { |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 5586 | const struct nand_manufacturer *manufacturer; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 5587 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5588 | int busw, ret; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 5589 | u8 *id_data = chip->id.data; |
| 5590 | u8 maf_id, dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5591 | |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 5592 | /* |
| 5593 | * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5594 | * after power-up. |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 5595 | */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5596 | ret = nand_reset(chip, 0); |
| 5597 | if (ret) |
| 5598 | return ret; |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 5599 | |
| 5600 | /* Select the device */ |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 5601 | chip->select_chip(chip, 0); |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 5602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5603 | /* Send the command for reading device ID */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5604 | ret = nand_readid_op(chip, 0, id_data, 2); |
| 5605 | if (ret) |
| 5606 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5607 | |
| 5608 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5609 | maf_id = id_data[0]; |
| 5610 | dev_id = id_data[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5611 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5612 | /* |
| 5613 | * 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] | 5614 | * interface concerns can cause random data which looks like a |
| 5615 | * possibly credible NAND flash to appear. If the two results do |
| 5616 | * not match, ignore the device completely. |
| 5617 | */ |
| 5618 | |
Brian Norris | 4aef9b7 | 2012-09-24 20:40:48 -0700 | [diff] [blame] | 5619 | /* Read entire ID string */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5620 | ret = nand_readid_op(chip, 0, id_data, sizeof(chip->id.data)); |
| 5621 | if (ret) |
| 5622 | return ret; |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 5623 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 5624 | if (id_data[0] != maf_id || id_data[1] != dev_id) { |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 5625 | 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] | 5626 | maf_id, dev_id, id_data[0], id_data[1]); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 5627 | return -ENODEV; |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 5628 | } |
| 5629 | |
Jean-Louis Thekekara | 5158bd5 | 2017-06-29 19:08:30 +0200 | [diff] [blame] | 5630 | chip->id.len = nand_id_len(id_data, ARRAY_SIZE(chip->id.data)); |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 5631 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5632 | /* Try to identify manufacturer */ |
| 5633 | manufacturer = nand_get_manufacturer(maf_id); |
| 5634 | chip->manufacturer.desc = manufacturer; |
| 5635 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5636 | if (!type) |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 5637 | type = nand_flash_ids; |
| 5638 | |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5639 | /* |
| 5640 | * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic |
| 5641 | * override it. |
| 5642 | * This is required to make sure initial NAND bus width set by the |
| 5643 | * NAND controller driver is coherent with the real NAND bus width |
| 5644 | * (extracted by auto-detection code). |
| 5645 | */ |
| 5646 | busw = chip->options & NAND_BUSWIDTH_16; |
| 5647 | |
| 5648 | /* |
| 5649 | * The flag is only set (never cleared), reset it to its default value |
| 5650 | * before starting auto-detection. |
| 5651 | */ |
| 5652 | chip->options &= ~NAND_BUSWIDTH_16; |
| 5653 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 5654 | for (; type->name != NULL; type++) { |
| 5655 | if (is_full_id_nand(type)) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5656 | if (find_full_id_nand(chip, type)) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 5657 | goto ident_done; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 5658 | } else if (dev_id == type->dev_id) { |
Brian Norris | db5b09f | 2015-05-22 10:43:12 -0700 | [diff] [blame] | 5659 | break; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 5660 | } |
| 5661 | } |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 5662 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 5663 | if (!type->name || !type->pagesize) { |
Masahiro Yamada | 35fc519 | 2014-04-09 16:26:26 +0900 | [diff] [blame] | 5664 | /* Check if the chip is ONFI compliant */ |
Miquel Raynal | bd0b643 | 2018-03-19 14:47:31 +0100 | [diff] [blame] | 5665 | ret = nand_flash_detect_onfi(chip); |
| 5666 | if (ret < 0) |
| 5667 | return ret; |
| 5668 | else if (ret) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 5669 | goto ident_done; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5670 | |
| 5671 | /* Check if the chip is JEDEC compliant */ |
Miquel Raynal | 480139d | 2018-03-19 14:47:30 +0100 | [diff] [blame] | 5672 | ret = nand_flash_detect_jedec(chip); |
| 5673 | if (ret < 0) |
| 5674 | return ret; |
| 5675 | else if (ret) |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 5676 | goto ident_done; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 5677 | } |
| 5678 | |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 5679 | if (!type->name) |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 5680 | return -ENODEV; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5681 | |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5682 | chip->parameters.model = kstrdup(type->name, GFP_KERNEL); |
| 5683 | if (!chip->parameters.model) |
| 5684 | return -ENOMEM; |
Thomas Gleixner | ba0251fe | 2006-05-27 01:02:13 +0200 | [diff] [blame] | 5685 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 5686 | chip->chipsize = (uint64_t)type->chipsize << 20; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5687 | |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5688 | if (!type->pagesize) |
| 5689 | nand_manufacturer_detect(chip); |
| 5690 | else |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5691 | nand_decode_id(chip, type); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 5692 | |
Brian Norris | bf7a01b | 2012-07-13 09:28:24 -0700 | [diff] [blame] | 5693 | /* Get chip options */ |
| 5694 | chip->options |= type->options; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 5695 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 5696 | ident_done: |
Miquel Raynal | f4531b2 | 2018-03-19 14:47:26 +0100 | [diff] [blame] | 5697 | if (!mtd->name) |
| 5698 | mtd->name = chip->parameters.model; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 5699 | |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 5700 | if (chip->options & NAND_BUSWIDTH_AUTO) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5701 | WARN_ON(busw & NAND_BUSWIDTH_16); |
| 5702 | nand_set_defaults(chip); |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 5703 | } else if (busw != (chip->options & NAND_BUSWIDTH_16)) { |
| 5704 | /* |
| 5705 | * Check, if buswidth is correct. Hardware drivers should set |
| 5706 | * chip correct! |
| 5707 | */ |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 5708 | 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] | 5709 | maf_id, dev_id); |
Boris Brezillon | bcc678c | 2017-01-07 15:48:25 +0100 | [diff] [blame] | 5710 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 5711 | mtd->name); |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5712 | pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8, |
| 5713 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8); |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5714 | ret = -EINVAL; |
| 5715 | |
| 5716 | goto free_detect_allocation; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5717 | } |
| 5718 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 5719 | nand_decode_bbm_options(chip); |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 5720 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5721 | /* Calculate the address shift from the page size */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5722 | chip->page_shift = ffs(mtd->writesize) - 1; |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5723 | /* Convert chipsize to number of pages per chip -1 */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5724 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5725 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5726 | chip->bbt_erase_shift = chip->phys_erase_shift = |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5727 | ffs(mtd->erasesize) - 1; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 5728 | if (chip->chipsize & 0xffffffff) |
| 5729 | chip->chip_shift = ffs((unsigned)chip->chipsize) - 1; |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 5730 | else { |
| 5731 | chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)); |
| 5732 | chip->chip_shift += 32 - 1; |
| 5733 | } |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5734 | |
Masahiro Yamada | 14157f8 | 2017-09-13 11:05:50 +0900 | [diff] [blame] | 5735 | if (chip->chip_shift - chip->page_shift > 16) |
| 5736 | chip->options |= NAND_ROW_ADDR_3; |
| 5737 | |
Artem Bityutskiy | 26d9be1 | 2011-04-28 20:26:59 +0300 | [diff] [blame] | 5738 | chip->badblockbits = 8; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5739 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5740 | /* Do not replace user supplied command function! */ |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 5741 | if (mtd->writesize > 512 && chip->legacy.cmdfunc == nand_command) |
| 5742 | chip->legacy.cmdfunc = nand_command_lp; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5743 | |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 5744 | 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] | 5745 | maf_id, dev_id); |
Miquel Raynal | f4531b2 | 2018-03-19 14:47:26 +0100 | [diff] [blame] | 5746 | pr_info("%s %s\n", nand_manufacturer_name(manufacturer), |
| 5747 | chip->parameters.model); |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 5748 | 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] | 5749 | (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC", |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 5750 | mtd->erasesize >> 10, mtd->writesize, mtd->oobsize); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 5751 | return 0; |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5752 | |
| 5753 | free_detect_allocation: |
| 5754 | kfree(chip->parameters.model); |
| 5755 | |
| 5756 | return ret; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5757 | } |
| 5758 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 5759 | static const char * const nand_ecc_modes[] = { |
| 5760 | [NAND_ECC_NONE] = "none", |
| 5761 | [NAND_ECC_SOFT] = "soft", |
| 5762 | [NAND_ECC_HW] = "hw", |
| 5763 | [NAND_ECC_HW_SYNDROME] = "hw_syndrome", |
| 5764 | [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first", |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 5765 | [NAND_ECC_ON_DIE] = "on-die", |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 5766 | }; |
| 5767 | |
| 5768 | static int of_get_nand_ecc_mode(struct device_node *np) |
| 5769 | { |
| 5770 | const char *pm; |
| 5771 | int err, i; |
| 5772 | |
| 5773 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 5774 | if (err < 0) |
| 5775 | return err; |
| 5776 | |
| 5777 | for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++) |
| 5778 | if (!strcasecmp(pm, nand_ecc_modes[i])) |
| 5779 | return i; |
| 5780 | |
Rafał Miłecki | ae211bc | 2016-04-17 22:53:06 +0200 | [diff] [blame] | 5781 | /* |
| 5782 | * For backward compatibility we support few obsoleted values that don't |
| 5783 | * have their mappings into nand_ecc_modes_t anymore (they were merged |
| 5784 | * with other enums). |
| 5785 | */ |
| 5786 | if (!strcasecmp(pm, "soft_bch")) |
| 5787 | return NAND_ECC_SOFT; |
| 5788 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 5789 | return -ENODEV; |
| 5790 | } |
| 5791 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 5792 | static const char * const nand_ecc_algos[] = { |
| 5793 | [NAND_ECC_HAMMING] = "hamming", |
| 5794 | [NAND_ECC_BCH] = "bch", |
Stefan Agner | f308d73 | 2018-06-24 23:27:22 +0200 | [diff] [blame] | 5795 | [NAND_ECC_RS] = "rs", |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 5796 | }; |
| 5797 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 5798 | static int of_get_nand_ecc_algo(struct device_node *np) |
| 5799 | { |
| 5800 | const char *pm; |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 5801 | int err, i; |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 5802 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 5803 | err = of_property_read_string(np, "nand-ecc-algo", &pm); |
| 5804 | if (!err) { |
| 5805 | for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++) |
| 5806 | if (!strcasecmp(pm, nand_ecc_algos[i])) |
| 5807 | return i; |
| 5808 | return -ENODEV; |
| 5809 | } |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 5810 | |
| 5811 | /* |
| 5812 | * For backward compatibility we also read "nand-ecc-mode" checking |
| 5813 | * for some obsoleted values that were specifying ECC algorithm. |
| 5814 | */ |
| 5815 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 5816 | if (err < 0) |
| 5817 | return err; |
| 5818 | |
| 5819 | if (!strcasecmp(pm, "soft")) |
| 5820 | return NAND_ECC_HAMMING; |
| 5821 | else if (!strcasecmp(pm, "soft_bch")) |
| 5822 | return NAND_ECC_BCH; |
| 5823 | |
| 5824 | return -ENODEV; |
| 5825 | } |
| 5826 | |
| 5827 | static int of_get_nand_ecc_step_size(struct device_node *np) |
| 5828 | { |
| 5829 | int ret; |
| 5830 | u32 val; |
| 5831 | |
| 5832 | ret = of_property_read_u32(np, "nand-ecc-step-size", &val); |
| 5833 | return ret ? ret : val; |
| 5834 | } |
| 5835 | |
| 5836 | static int of_get_nand_ecc_strength(struct device_node *np) |
| 5837 | { |
| 5838 | int ret; |
| 5839 | u32 val; |
| 5840 | |
| 5841 | ret = of_property_read_u32(np, "nand-ecc-strength", &val); |
| 5842 | return ret ? ret : val; |
| 5843 | } |
| 5844 | |
| 5845 | static int of_get_nand_bus_width(struct device_node *np) |
| 5846 | { |
| 5847 | u32 val; |
| 5848 | |
| 5849 | if (of_property_read_u32(np, "nand-bus-width", &val)) |
| 5850 | return 8; |
| 5851 | |
| 5852 | switch (val) { |
| 5853 | case 8: |
| 5854 | case 16: |
| 5855 | return val; |
| 5856 | default: |
| 5857 | return -EIO; |
| 5858 | } |
| 5859 | } |
| 5860 | |
| 5861 | static bool of_get_nand_on_flash_bbt(struct device_node *np) |
| 5862 | { |
| 5863 | return of_property_read_bool(np, "nand-on-flash-bbt"); |
| 5864 | } |
| 5865 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 5866 | static int nand_dt_init(struct nand_chip *chip) |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 5867 | { |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 5868 | struct device_node *dn = nand_get_flash_node(chip); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 5869 | int ecc_mode, ecc_algo, ecc_strength, ecc_step; |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 5870 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 5871 | if (!dn) |
| 5872 | return 0; |
| 5873 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 5874 | if (of_get_nand_bus_width(dn) == 16) |
| 5875 | chip->options |= NAND_BUSWIDTH_16; |
| 5876 | |
Stefan Agner | f922bd7 | 2018-06-24 23:27:23 +0200 | [diff] [blame] | 5877 | if (of_property_read_bool(dn, "nand-is-boot-medium")) |
| 5878 | chip->options |= NAND_IS_BOOT_MEDIUM; |
| 5879 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 5880 | if (of_get_nand_on_flash_bbt(dn)) |
| 5881 | chip->bbt_options |= NAND_BBT_USE_FLASH; |
| 5882 | |
| 5883 | ecc_mode = of_get_nand_ecc_mode(dn); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 5884 | ecc_algo = of_get_nand_ecc_algo(dn); |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 5885 | ecc_strength = of_get_nand_ecc_strength(dn); |
| 5886 | ecc_step = of_get_nand_ecc_step_size(dn); |
| 5887 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 5888 | if (ecc_mode >= 0) |
| 5889 | chip->ecc.mode = ecc_mode; |
| 5890 | |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 5891 | if (ecc_algo >= 0) |
| 5892 | chip->ecc.algo = ecc_algo; |
| 5893 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 5894 | if (ecc_strength >= 0) |
| 5895 | chip->ecc.strength = ecc_strength; |
| 5896 | |
| 5897 | if (ecc_step > 0) |
| 5898 | chip->ecc.size = ecc_step; |
| 5899 | |
Boris Brezillon | ba78ee0 | 2016-06-08 17:04:22 +0200 | [diff] [blame] | 5900 | if (of_property_read_bool(dn, "nand-ecc-maximize")) |
| 5901 | chip->ecc.options |= NAND_ECC_MAXIMIZE; |
| 5902 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 5903 | return 0; |
| 5904 | } |
| 5905 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5906 | /** |
Miquel Raynal | 98732da | 2018-07-25 15:31:50 +0200 | [diff] [blame] | 5907 | * nand_scan_ident - Scan for the NAND device |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5908 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5909 | * @maxchips: number of chips to scan for |
| 5910 | * @table: alternative NAND ID table |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5911 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 5912 | * This is the first phase of the normal nand_scan() function. It reads the |
| 5913 | * flash ID and sets up MTD fields accordingly. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5914 | * |
Miquel Raynal | 98732da | 2018-07-25 15:31:50 +0200 | [diff] [blame] | 5915 | * This helper used to be called directly from controller drivers that needed |
| 5916 | * to tweak some ECC-related parameters before nand_scan_tail(). This separation |
| 5917 | * prevented dynamic allocations during this phase which was unconvenient and |
| 5918 | * as been banned for the benefit of the ->init_ecc()/cleanup_ecc() hooks. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5919 | */ |
Boris Brezillon | 871a407 | 2018-08-04 22:59:22 +0200 | [diff] [blame] | 5920 | static int nand_scan_ident(struct nand_chip *chip, unsigned int maxchips, |
Miquel Raynal | 98732da | 2018-07-25 15:31:50 +0200 | [diff] [blame] | 5921 | struct nand_flash_dev *table) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5922 | { |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 5923 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 871a407 | 2018-08-04 22:59:22 +0200 | [diff] [blame] | 5924 | int nand_maf_id, nand_dev_id; |
| 5925 | unsigned int i; |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 5926 | int ret; |
| 5927 | |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 5928 | /* Enforce the right timings for reset/detection */ |
| 5929 | onfi_fill_data_interface(chip, NAND_SDR_IFACE, 0); |
| 5930 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 5931 | ret = nand_dt_init(chip); |
| 5932 | if (ret) |
| 5933 | return ret; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5934 | |
Brian Norris | f7a8e38 | 2016-01-05 10:39:45 -0800 | [diff] [blame] | 5935 | if (!mtd->name && mtd->dev.parent) |
| 5936 | mtd->name = dev_name(mtd->dev.parent); |
| 5937 | |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 5938 | /* |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 5939 | * ->legacy.cmdfunc() is legacy and will only be used if ->exec_op() is |
| 5940 | * not populated. |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 5941 | */ |
| 5942 | if (!chip->exec_op) { |
Andrey Smirnov | 76fe334 | 2016-07-21 14:59:20 -0700 | [diff] [blame] | 5943 | /* |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 5944 | * Default functions assigned for ->legacy.cmdfunc() and |
| 5945 | * ->select_chip() both expect ->legacy.cmd_ctrl() to be |
| 5946 | * populated. |
Andrey Smirnov | 76fe334 | 2016-07-21 14:59:20 -0700 | [diff] [blame] | 5947 | */ |
Boris Brezillon | bf6065c | 2018-09-07 00:38:36 +0200 | [diff] [blame] | 5948 | if ((!chip->legacy.cmdfunc || !chip->select_chip) && |
| 5949 | !chip->legacy.cmd_ctrl) { |
| 5950 | pr_err("->legacy.cmd_ctrl() should be provided\n"); |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 5951 | return -EINVAL; |
| 5952 | } |
Andrey Smirnov | 76fe334 | 2016-07-21 14:59:20 -0700 | [diff] [blame] | 5953 | } |
Miquel Raynal | 8878b12 | 2017-11-09 14:16:45 +0100 | [diff] [blame] | 5954 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5955 | /* Set the default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 5956 | nand_set_defaults(chip); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5957 | |
| 5958 | /* Read the flash type */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame] | 5959 | ret = nand_detect(chip, table); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 5960 | if (ret) { |
Ben Dooks | b1c6e6d | 2009-11-02 18:12:33 +0000 | [diff] [blame] | 5961 | if (!(chip->options & NAND_SCAN_SILENT_NODEV)) |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 5962 | pr_warn("No NAND device found\n"); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 5963 | chip->select_chip(chip, -1); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 5964 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5965 | } |
| 5966 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 5967 | nand_maf_id = chip->id.data[0]; |
| 5968 | nand_dev_id = chip->id.data[1]; |
| 5969 | |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 5970 | chip->select_chip(chip, -1); |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 5971 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 5972 | /* Check for a chip array */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 5973 | for (i = 1; i < maxchips; i++) { |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5974 | u8 id[2]; |
| 5975 | |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 5976 | /* See comment in nand_get_flash_type for reset */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 5977 | nand_reset(chip, i); |
| 5978 | |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 5979 | chip->select_chip(chip, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5980 | /* Send the command for reading device ID */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5981 | nand_readid_op(chip, 0, id, sizeof(id)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5982 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 5983 | if (nand_maf_id != id[0] || nand_dev_id != id[1]) { |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 5984 | chip->select_chip(chip, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5985 | break; |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 5986 | } |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 5987 | chip->select_chip(chip, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5988 | } |
| 5989 | if (i > 1) |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 5990 | pr_info("%d chips detected\n", i); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5991 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5992 | /* Store the number of chips and calc total size for mtd */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 5993 | chip->numchips = i; |
| 5994 | mtd->size = i * chip->chipsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5995 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 5996 | return 0; |
| 5997 | } |
| 5998 | |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 5999 | static void nand_scan_ident_cleanup(struct nand_chip *chip) |
| 6000 | { |
| 6001 | kfree(chip->parameters.model); |
Miquel Raynal | 3d3fe3c | 2018-07-25 15:31:52 +0200 | [diff] [blame] | 6002 | kfree(chip->parameters.onfi); |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 6003 | } |
| 6004 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 6005 | static int nand_set_ecc_soft_ops(struct mtd_info *mtd) |
| 6006 | { |
| 6007 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 6008 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 6009 | |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 6010 | if (WARN_ON(ecc->mode != NAND_ECC_SOFT)) |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 6011 | return -EINVAL; |
| 6012 | |
| 6013 | switch (ecc->algo) { |
| 6014 | case NAND_ECC_HAMMING: |
| 6015 | ecc->calculate = nand_calculate_ecc; |
| 6016 | ecc->correct = nand_correct_data; |
| 6017 | ecc->read_page = nand_read_page_swecc; |
| 6018 | ecc->read_subpage = nand_read_subpage; |
| 6019 | ecc->write_page = nand_write_page_swecc; |
| 6020 | ecc->read_page_raw = nand_read_page_raw; |
| 6021 | ecc->write_page_raw = nand_write_page_raw; |
| 6022 | ecc->read_oob = nand_read_oob_std; |
| 6023 | ecc->write_oob = nand_write_oob_std; |
| 6024 | if (!ecc->size) |
| 6025 | ecc->size = 256; |
| 6026 | ecc->bytes = 3; |
| 6027 | ecc->strength = 1; |
| 6028 | return 0; |
| 6029 | case NAND_ECC_BCH: |
| 6030 | if (!mtd_nand_has_bch()) { |
| 6031 | WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n"); |
| 6032 | return -EINVAL; |
| 6033 | } |
| 6034 | ecc->calculate = nand_bch_calculate_ecc; |
| 6035 | ecc->correct = nand_bch_correct_data; |
| 6036 | ecc->read_page = nand_read_page_swecc; |
| 6037 | ecc->read_subpage = nand_read_subpage; |
| 6038 | ecc->write_page = nand_write_page_swecc; |
| 6039 | ecc->read_page_raw = nand_read_page_raw; |
| 6040 | ecc->write_page_raw = nand_write_page_raw; |
| 6041 | ecc->read_oob = nand_read_oob_std; |
| 6042 | ecc->write_oob = nand_write_oob_std; |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 6043 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 6044 | /* |
| 6045 | * Board driver should supply ecc.size and ecc.strength |
| 6046 | * values to select how many bits are correctable. |
| 6047 | * Otherwise, default to 4 bits for large page devices. |
| 6048 | */ |
| 6049 | if (!ecc->size && (mtd->oobsize >= 64)) { |
| 6050 | ecc->size = 512; |
| 6051 | ecc->strength = 4; |
| 6052 | } |
| 6053 | |
| 6054 | /* |
| 6055 | * if no ecc placement scheme was provided pickup the default |
| 6056 | * large page one. |
| 6057 | */ |
| 6058 | if (!mtd->ooblayout) { |
| 6059 | /* handle large page devices only */ |
| 6060 | if (mtd->oobsize < 64) { |
| 6061 | WARN(1, "OOB layout is required when using software BCH on small pages\n"); |
| 6062 | return -EINVAL; |
| 6063 | } |
| 6064 | |
| 6065 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops); |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 6066 | |
| 6067 | } |
| 6068 | |
| 6069 | /* |
| 6070 | * We can only maximize ECC config when the default layout is |
| 6071 | * used, otherwise we don't know how many bytes can really be |
| 6072 | * used. |
| 6073 | */ |
| 6074 | if (mtd->ooblayout == &nand_ooblayout_lp_ops && |
| 6075 | ecc->options & NAND_ECC_MAXIMIZE) { |
| 6076 | int steps, bytes; |
| 6077 | |
| 6078 | /* Always prefer 1k blocks over 512bytes ones */ |
| 6079 | ecc->size = 1024; |
| 6080 | steps = mtd->writesize / ecc->size; |
| 6081 | |
| 6082 | /* Reserve 2 bytes for the BBM */ |
| 6083 | bytes = (mtd->oobsize - 2) / steps; |
| 6084 | ecc->strength = bytes * 8 / fls(8 * ecc->size); |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 6085 | } |
| 6086 | |
| 6087 | /* See nand_bch_init() for details. */ |
| 6088 | ecc->bytes = 0; |
| 6089 | ecc->priv = nand_bch_init(mtd); |
| 6090 | if (!ecc->priv) { |
| 6091 | WARN(1, "BCH ECC initialization failed!\n"); |
| 6092 | return -EINVAL; |
| 6093 | } |
| 6094 | return 0; |
| 6095 | default: |
| 6096 | WARN(1, "Unsupported ECC algorithm!\n"); |
| 6097 | return -EINVAL; |
| 6098 | } |
| 6099 | } |
| 6100 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 6101 | /** |
| 6102 | * nand_check_ecc_caps - check the sanity of preset ECC settings |
| 6103 | * @chip: nand chip info structure |
| 6104 | * @caps: ECC caps info structure |
| 6105 | * @oobavail: OOB size that the ECC engine can use |
| 6106 | * |
| 6107 | * When ECC step size and strength are already set, check if they are supported |
| 6108 | * by the controller and the calculated ECC bytes fit within the chip's OOB. |
| 6109 | * On success, the calculated ECC bytes is set. |
| 6110 | */ |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 6111 | static int |
| 6112 | nand_check_ecc_caps(struct nand_chip *chip, |
| 6113 | const struct nand_ecc_caps *caps, int oobavail) |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 6114 | { |
| 6115 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 6116 | const struct nand_ecc_step_info *stepinfo; |
| 6117 | int preset_step = chip->ecc.size; |
| 6118 | int preset_strength = chip->ecc.strength; |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 6119 | int ecc_bytes, nsteps = mtd->writesize / preset_step; |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 6120 | int i, j; |
| 6121 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 6122 | for (i = 0; i < caps->nstepinfos; i++) { |
| 6123 | stepinfo = &caps->stepinfos[i]; |
| 6124 | |
| 6125 | if (stepinfo->stepsize != preset_step) |
| 6126 | continue; |
| 6127 | |
| 6128 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 6129 | if (stepinfo->strengths[j] != preset_strength) |
| 6130 | continue; |
| 6131 | |
| 6132 | ecc_bytes = caps->calc_ecc_bytes(preset_step, |
| 6133 | preset_strength); |
| 6134 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 6135 | return ecc_bytes; |
| 6136 | |
| 6137 | if (ecc_bytes * nsteps > oobavail) { |
| 6138 | pr_err("ECC (step, strength) = (%d, %d) does not fit in OOB", |
| 6139 | preset_step, preset_strength); |
| 6140 | return -ENOSPC; |
| 6141 | } |
| 6142 | |
| 6143 | chip->ecc.bytes = ecc_bytes; |
| 6144 | |
| 6145 | return 0; |
| 6146 | } |
| 6147 | } |
| 6148 | |
| 6149 | pr_err("ECC (step, strength) = (%d, %d) not supported on this controller", |
| 6150 | preset_step, preset_strength); |
| 6151 | |
| 6152 | return -ENOTSUPP; |
| 6153 | } |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 6154 | |
| 6155 | /** |
| 6156 | * nand_match_ecc_req - meet the chip's requirement with least ECC bytes |
| 6157 | * @chip: nand chip info structure |
| 6158 | * @caps: ECC engine caps info structure |
| 6159 | * @oobavail: OOB size that the ECC engine can use |
| 6160 | * |
| 6161 | * If a chip's ECC requirement is provided, try to meet it with the least |
| 6162 | * number of ECC bytes (i.e. with the largest number of OOB-free bytes). |
| 6163 | * On success, the chosen ECC settings are set. |
| 6164 | */ |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 6165 | static int |
| 6166 | nand_match_ecc_req(struct nand_chip *chip, |
| 6167 | const struct nand_ecc_caps *caps, int oobavail) |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 6168 | { |
| 6169 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 6170 | const struct nand_ecc_step_info *stepinfo; |
| 6171 | int req_step = chip->ecc_step_ds; |
| 6172 | int req_strength = chip->ecc_strength_ds; |
| 6173 | int req_corr, step_size, strength, nsteps, ecc_bytes, ecc_bytes_total; |
| 6174 | int best_step, best_strength, best_ecc_bytes; |
| 6175 | int best_ecc_bytes_total = INT_MAX; |
| 6176 | int i, j; |
| 6177 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 6178 | /* No information provided by the NAND chip */ |
| 6179 | if (!req_step || !req_strength) |
| 6180 | return -ENOTSUPP; |
| 6181 | |
| 6182 | /* number of correctable bits the chip requires in a page */ |
| 6183 | req_corr = mtd->writesize / req_step * req_strength; |
| 6184 | |
| 6185 | for (i = 0; i < caps->nstepinfos; i++) { |
| 6186 | stepinfo = &caps->stepinfos[i]; |
| 6187 | step_size = stepinfo->stepsize; |
| 6188 | |
| 6189 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 6190 | strength = stepinfo->strengths[j]; |
| 6191 | |
| 6192 | /* |
| 6193 | * If both step size and strength are smaller than the |
| 6194 | * chip's requirement, it is not easy to compare the |
| 6195 | * resulted reliability. |
| 6196 | */ |
| 6197 | if (step_size < req_step && strength < req_strength) |
| 6198 | continue; |
| 6199 | |
| 6200 | if (mtd->writesize % step_size) |
| 6201 | continue; |
| 6202 | |
| 6203 | nsteps = mtd->writesize / step_size; |
| 6204 | |
| 6205 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 6206 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 6207 | continue; |
| 6208 | ecc_bytes_total = ecc_bytes * nsteps; |
| 6209 | |
| 6210 | if (ecc_bytes_total > oobavail || |
| 6211 | strength * nsteps < req_corr) |
| 6212 | continue; |
| 6213 | |
| 6214 | /* |
| 6215 | * We assume the best is to meet the chip's requrement |
| 6216 | * with the least number of ECC bytes. |
| 6217 | */ |
| 6218 | if (ecc_bytes_total < best_ecc_bytes_total) { |
| 6219 | best_ecc_bytes_total = ecc_bytes_total; |
| 6220 | best_step = step_size; |
| 6221 | best_strength = strength; |
| 6222 | best_ecc_bytes = ecc_bytes; |
| 6223 | } |
| 6224 | } |
| 6225 | } |
| 6226 | |
| 6227 | if (best_ecc_bytes_total == INT_MAX) |
| 6228 | return -ENOTSUPP; |
| 6229 | |
| 6230 | chip->ecc.size = best_step; |
| 6231 | chip->ecc.strength = best_strength; |
| 6232 | chip->ecc.bytes = best_ecc_bytes; |
| 6233 | |
| 6234 | return 0; |
| 6235 | } |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 6236 | |
| 6237 | /** |
| 6238 | * nand_maximize_ecc - choose the max ECC strength available |
| 6239 | * @chip: nand chip info structure |
| 6240 | * @caps: ECC engine caps info structure |
| 6241 | * @oobavail: OOB size that the ECC engine can use |
| 6242 | * |
| 6243 | * Choose the max ECC strength that is supported on the controller, and can fit |
| 6244 | * within the chip's OOB. On success, the chosen ECC settings are set. |
| 6245 | */ |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 6246 | static int |
| 6247 | nand_maximize_ecc(struct nand_chip *chip, |
| 6248 | const struct nand_ecc_caps *caps, int oobavail) |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 6249 | { |
| 6250 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 6251 | const struct nand_ecc_step_info *stepinfo; |
| 6252 | int step_size, strength, nsteps, ecc_bytes, corr; |
| 6253 | int best_corr = 0; |
| 6254 | int best_step = 0; |
| 6255 | int best_strength, best_ecc_bytes; |
| 6256 | int i, j; |
| 6257 | |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 6258 | for (i = 0; i < caps->nstepinfos; i++) { |
| 6259 | stepinfo = &caps->stepinfos[i]; |
| 6260 | step_size = stepinfo->stepsize; |
| 6261 | |
| 6262 | /* If chip->ecc.size is already set, respect it */ |
| 6263 | if (chip->ecc.size && step_size != chip->ecc.size) |
| 6264 | continue; |
| 6265 | |
| 6266 | for (j = 0; j < stepinfo->nstrengths; j++) { |
| 6267 | strength = stepinfo->strengths[j]; |
| 6268 | |
| 6269 | if (mtd->writesize % step_size) |
| 6270 | continue; |
| 6271 | |
| 6272 | nsteps = mtd->writesize / step_size; |
| 6273 | |
| 6274 | ecc_bytes = caps->calc_ecc_bytes(step_size, strength); |
| 6275 | if (WARN_ON_ONCE(ecc_bytes < 0)) |
| 6276 | continue; |
| 6277 | |
| 6278 | if (ecc_bytes * nsteps > oobavail) |
| 6279 | continue; |
| 6280 | |
| 6281 | corr = strength * nsteps; |
| 6282 | |
| 6283 | /* |
| 6284 | * If the number of correctable bits is the same, |
| 6285 | * bigger step_size has more reliability. |
| 6286 | */ |
| 6287 | if (corr > best_corr || |
| 6288 | (corr == best_corr && step_size > best_step)) { |
| 6289 | best_corr = corr; |
| 6290 | best_step = step_size; |
| 6291 | best_strength = strength; |
| 6292 | best_ecc_bytes = ecc_bytes; |
| 6293 | } |
| 6294 | } |
| 6295 | } |
| 6296 | |
| 6297 | if (!best_corr) |
| 6298 | return -ENOTSUPP; |
| 6299 | |
| 6300 | chip->ecc.size = best_step; |
| 6301 | chip->ecc.strength = best_strength; |
| 6302 | chip->ecc.bytes = best_ecc_bytes; |
| 6303 | |
| 6304 | return 0; |
| 6305 | } |
Masahiro Yamada | 2c8f8af | 2017-06-07 20:52:10 +0900 | [diff] [blame] | 6306 | |
Abhishek Sahu | 181ace9 | 2018-06-20 12:57:28 +0530 | [diff] [blame] | 6307 | /** |
| 6308 | * nand_ecc_choose_conf - Set the ECC strength and ECC step size |
| 6309 | * @chip: nand chip info structure |
| 6310 | * @caps: ECC engine caps info structure |
| 6311 | * @oobavail: OOB size that the ECC engine can use |
| 6312 | * |
| 6313 | * Choose the ECC configuration according to following logic |
| 6314 | * |
| 6315 | * 1. If both ECC step size and ECC strength are already set (usually by DT) |
| 6316 | * then check if it is supported by this controller. |
| 6317 | * 2. If NAND_ECC_MAXIMIZE is set, then select maximum ECC strength. |
| 6318 | * 3. Otherwise, try to match the ECC step size and ECC strength closest |
| 6319 | * to the chip's requirement. If available OOB size can't fit the chip |
| 6320 | * requirement then fallback to the maximum ECC step size and ECC strength. |
| 6321 | * |
| 6322 | * On success, the chosen ECC settings are set. |
| 6323 | */ |
| 6324 | int nand_ecc_choose_conf(struct nand_chip *chip, |
| 6325 | const struct nand_ecc_caps *caps, int oobavail) |
| 6326 | { |
Abhishek Sahu | 0cf5c7d | 2018-06-20 12:57:42 +0530 | [diff] [blame] | 6327 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 6328 | |
| 6329 | if (WARN_ON(oobavail < 0 || oobavail > mtd->oobsize)) |
| 6330 | return -EINVAL; |
| 6331 | |
Abhishek Sahu | 181ace9 | 2018-06-20 12:57:28 +0530 | [diff] [blame] | 6332 | if (chip->ecc.size && chip->ecc.strength) |
| 6333 | return nand_check_ecc_caps(chip, caps, oobavail); |
| 6334 | |
| 6335 | if (chip->ecc.options & NAND_ECC_MAXIMIZE) |
| 6336 | return nand_maximize_ecc(chip, caps, oobavail); |
| 6337 | |
| 6338 | if (!nand_match_ecc_req(chip, caps, oobavail)) |
| 6339 | return 0; |
| 6340 | |
| 6341 | return nand_maximize_ecc(chip, caps, oobavail); |
| 6342 | } |
| 6343 | EXPORT_SYMBOL_GPL(nand_ecc_choose_conf); |
| 6344 | |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 6345 | /* |
| 6346 | * Check if the chip configuration meet the datasheet requirements. |
| 6347 | |
| 6348 | * If our configuration corrects A bits per B bytes and the minimum |
| 6349 | * required correction level is X bits per Y bytes, then we must ensure |
| 6350 | * both of the following are true: |
| 6351 | * |
| 6352 | * (1) A / B >= X / Y |
| 6353 | * (2) A >= X |
| 6354 | * |
| 6355 | * Requirement (1) ensures we can correct for the required bitflip density. |
| 6356 | * Requirement (2) ensures we can correct even when all bitflips are clumped |
| 6357 | * in the same sector. |
| 6358 | */ |
| 6359 | static bool nand_ecc_strength_good(struct mtd_info *mtd) |
| 6360 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 6361 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 6362 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 6363 | int corr, ds_corr; |
| 6364 | |
| 6365 | if (ecc->size == 0 || chip->ecc_step_ds == 0) |
| 6366 | /* Not enough information */ |
| 6367 | return true; |
| 6368 | |
| 6369 | /* |
| 6370 | * We get the number of corrected bits per page to compare |
| 6371 | * the correction density. |
| 6372 | */ |
| 6373 | corr = (mtd->writesize * ecc->strength) / ecc->size; |
| 6374 | ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds; |
| 6375 | |
| 6376 | return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds; |
| 6377 | } |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 6378 | |
| 6379 | /** |
Miquel Raynal | 98732da | 2018-07-25 15:31:50 +0200 | [diff] [blame] | 6380 | * nand_scan_tail - Scan for the NAND device |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 6381 | * @chip: NAND chip object |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 6382 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 6383 | * This is the second phase of the normal nand_scan() function. It fills out |
| 6384 | * all the uninitialized function pointers with the defaults and scans for a |
| 6385 | * bad block table if appropriate. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 6386 | */ |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 6387 | static int nand_scan_tail(struct nand_chip *chip) |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 6388 | { |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 6389 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6390 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6391 | int ret, i; |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 6392 | |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 6393 | /* 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] | 6394 | if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) && |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 6395 | !(chip->bbt_options & NAND_BBT_USE_FLASH))) { |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6396 | return -EINVAL; |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 6397 | } |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 6398 | |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 6399 | chip->data_buf = kmalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL); |
Boris Brezillon | aeb93af | 2017-12-05 12:09:29 +0100 | [diff] [blame] | 6400 | if (!chip->data_buf) |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6401 | return -ENOMEM; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 6402 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6403 | /* |
| 6404 | * FIXME: some NAND manufacturer drivers expect the first die to be |
| 6405 | * selected when manufacturer->init() is called. They should be fixed |
| 6406 | * to explictly select the relevant die when interacting with the NAND |
| 6407 | * chip. |
| 6408 | */ |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 6409 | chip->select_chip(chip, 0); |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6410 | ret = nand_manufacturer_init(chip); |
Boris Brezillon | 758b56f | 2018-09-06 14:05:24 +0200 | [diff] [blame] | 6411 | chip->select_chip(chip, -1); |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6412 | if (ret) |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 6413 | goto err_free_buf; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6414 | |
David Woodhouse | 7dcdcbef | 2006-10-21 17:09:53 +0100 | [diff] [blame] | 6415 | /* Set the internal oob buffer location, just after the page data */ |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 6416 | chip->oob_poi = chip->data_buf + mtd->writesize; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 6417 | |
| 6418 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 6419 | * If no default placement scheme is given, select an appropriate one. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 6420 | */ |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 6421 | if (!mtd->ooblayout && |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 6422 | !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 6423 | switch (mtd->oobsize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6424 | case 8: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6425 | case 16: |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 6426 | mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6427 | break; |
| 6428 | case 64: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 6429 | case 128: |
Alexander Couzens | 6a623e0 | 2017-05-02 12:19:00 +0200 | [diff] [blame] | 6430 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_hamming_ops); |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 6431 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6432 | default: |
Miquel Raynal | 882fd15 | 2017-08-26 17:19:15 +0200 | [diff] [blame] | 6433 | /* |
| 6434 | * Expose the whole OOB area to users if ECC_NONE |
| 6435 | * is passed. We could do that for all kind of |
| 6436 | * ->oobsize, but we must keep the old large/small |
| 6437 | * page with ECC layout when ->oobsize <= 128 for |
| 6438 | * compatibility reasons. |
| 6439 | */ |
| 6440 | if (ecc->mode == NAND_ECC_NONE) { |
| 6441 | mtd_set_ooblayout(mtd, |
| 6442 | &nand_ooblayout_lp_ops); |
| 6443 | break; |
| 6444 | } |
| 6445 | |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 6446 | WARN(1, "No oob scheme defined for oobsize %d\n", |
| 6447 | mtd->oobsize); |
| 6448 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6449 | goto err_nand_manuf_cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6450 | } |
| 6451 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 6452 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 6453 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 6454 | * Check ECC mode, default to software if 3byte/512byte hardware ECC is |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 6455 | * selected and we have 256 byte pagesize fallback to software ECC |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 6456 | */ |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 6457 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6458 | switch (ecc->mode) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 6459 | case NAND_ECC_HW_OOB_FIRST: |
| 6460 | /* Similar to NAND_ECC_HW, but a separate read_page handle */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6461 | if (!ecc->calculate || !ecc->correct || !ecc->hwctl) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 6462 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 6463 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6464 | goto err_nand_manuf_cleanup; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 6465 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6466 | if (!ecc->read_page) |
| 6467 | ecc->read_page = nand_read_page_hwecc_oob_first; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 6468 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 6469 | case NAND_ECC_HW: |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 6470 | /* Use standard hwecc read page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6471 | if (!ecc->read_page) |
| 6472 | ecc->read_page = nand_read_page_hwecc; |
| 6473 | if (!ecc->write_page) |
| 6474 | ecc->write_page = nand_write_page_hwecc; |
| 6475 | if (!ecc->read_page_raw) |
| 6476 | ecc->read_page_raw = nand_read_page_raw; |
| 6477 | if (!ecc->write_page_raw) |
| 6478 | ecc->write_page_raw = nand_write_page_raw; |
| 6479 | if (!ecc->read_oob) |
| 6480 | ecc->read_oob = nand_read_oob_std; |
| 6481 | if (!ecc->write_oob) |
| 6482 | ecc->write_oob = nand_write_oob_std; |
| 6483 | if (!ecc->read_subpage) |
| 6484 | ecc->read_subpage = nand_read_subpage; |
Helmut Schaa | 44991b3 | 2014-04-09 11:13:24 +0200 | [diff] [blame] | 6485 | if (!ecc->write_subpage && ecc->hwctl && ecc->calculate) |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6486 | ecc->write_subpage = nand_write_subpage_hwecc; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 6487 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 6488 | case NAND_ECC_HW_SYNDROME: |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6489 | if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) && |
| 6490 | (!ecc->read_page || |
| 6491 | ecc->read_page == nand_read_page_hwecc || |
| 6492 | !ecc->write_page || |
| 6493 | ecc->write_page == nand_write_page_hwecc)) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 6494 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 6495 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6496 | goto err_nand_manuf_cleanup; |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 6497 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 6498 | /* Use standard syndrome read/write page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6499 | if (!ecc->read_page) |
| 6500 | ecc->read_page = nand_read_page_syndrome; |
| 6501 | if (!ecc->write_page) |
| 6502 | ecc->write_page = nand_write_page_syndrome; |
| 6503 | if (!ecc->read_page_raw) |
| 6504 | ecc->read_page_raw = nand_read_page_raw_syndrome; |
| 6505 | if (!ecc->write_page_raw) |
| 6506 | ecc->write_page_raw = nand_write_page_raw_syndrome; |
| 6507 | if (!ecc->read_oob) |
| 6508 | ecc->read_oob = nand_read_oob_syndrome; |
| 6509 | if (!ecc->write_oob) |
| 6510 | ecc->write_oob = nand_write_oob_syndrome; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 6511 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6512 | if (mtd->writesize >= ecc->size) { |
| 6513 | if (!ecc->strength) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 6514 | WARN(1, "Driver must set ecc.strength when using hardware ECC\n"); |
| 6515 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6516 | goto err_nand_manuf_cleanup; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 6517 | } |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 6518 | break; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 6519 | } |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 6520 | pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n", |
| 6521 | ecc->size, mtd->writesize); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6522 | ecc->mode = NAND_ECC_SOFT; |
Rafał Miłecki | e9d4fae | 2016-04-17 22:53:02 +0200 | [diff] [blame] | 6523 | ecc->algo = NAND_ECC_HAMMING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6524 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 6525 | case NAND_ECC_SOFT: |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 6526 | ret = nand_set_ecc_soft_ops(mtd); |
| 6527 | if (ret) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 6528 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6529 | goto err_nand_manuf_cleanup; |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 6530 | } |
| 6531 | break; |
| 6532 | |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 6533 | case NAND_ECC_ON_DIE: |
| 6534 | if (!ecc->read_page || !ecc->write_page) { |
| 6535 | WARN(1, "No ECC functions supplied; on-die ECC not possible\n"); |
| 6536 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6537 | goto err_nand_manuf_cleanup; |
Thomas Petazzoni | 785818f | 2017-04-29 11:06:43 +0200 | [diff] [blame] | 6538 | } |
| 6539 | if (!ecc->read_oob) |
| 6540 | ecc->read_oob = nand_read_oob_std; |
| 6541 | if (!ecc->write_oob) |
| 6542 | ecc->write_oob = nand_write_oob_std; |
| 6543 | break; |
| 6544 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 6545 | case NAND_ECC_NONE: |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 6546 | 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] | 6547 | ecc->read_page = nand_read_page_raw; |
| 6548 | ecc->write_page = nand_write_page_raw; |
| 6549 | ecc->read_oob = nand_read_oob_std; |
| 6550 | ecc->read_page_raw = nand_read_page_raw; |
| 6551 | ecc->write_page_raw = nand_write_page_raw; |
| 6552 | ecc->write_oob = nand_write_oob_std; |
| 6553 | ecc->size = mtd->writesize; |
| 6554 | ecc->bytes = 0; |
| 6555 | ecc->strength = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6556 | break; |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 6557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6558 | default: |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 6559 | WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode); |
| 6560 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6561 | goto err_nand_manuf_cleanup; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 6562 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6563 | |
Boris Brezillon | aeb93af | 2017-12-05 12:09:29 +0100 | [diff] [blame] | 6564 | if (ecc->correct || ecc->calculate) { |
| 6565 | ecc->calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL); |
| 6566 | ecc->code_buf = kmalloc(mtd->oobsize, GFP_KERNEL); |
| 6567 | if (!ecc->calc_buf || !ecc->code_buf) { |
| 6568 | ret = -ENOMEM; |
| 6569 | goto err_nand_manuf_cleanup; |
| 6570 | } |
| 6571 | } |
| 6572 | |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 6573 | /* For many systems, the standard OOB write also works for raw */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6574 | if (!ecc->read_oob_raw) |
| 6575 | ecc->read_oob_raw = ecc->read_oob; |
| 6576 | if (!ecc->write_oob_raw) |
| 6577 | ecc->write_oob_raw = ecc->write_oob; |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 6578 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 6579 | /* propagate ecc info to mtd_info */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 6580 | mtd->ecc_strength = ecc->strength; |
| 6581 | mtd->ecc_step_size = ecc->size; |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 6582 | |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 6583 | /* |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 6584 | * 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] | 6585 | * mode. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 6586 | */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6587 | ecc->steps = mtd->writesize / ecc->size; |
| 6588 | if (ecc->steps * ecc->size != mtd->writesize) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 6589 | WARN(1, "Invalid ECC parameters\n"); |
| 6590 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6591 | goto err_nand_manuf_cleanup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6592 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6593 | ecc->total = ecc->steps * ecc->bytes; |
Masahiro Yamada | 79e0348 | 2017-05-25 13:50:20 +0900 | [diff] [blame] | 6594 | if (ecc->total > mtd->oobsize) { |
| 6595 | WARN(1, "Total number of ECC bytes exceeded oobsize\n"); |
| 6596 | ret = -EINVAL; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6597 | goto err_nand_manuf_cleanup; |
Masahiro Yamada | 79e0348 | 2017-05-25 13:50:20 +0900 | [diff] [blame] | 6598 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 6599 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 6600 | /* |
| 6601 | * The number of bytes available for a client to place data into |
| 6602 | * the out of band area. |
| 6603 | */ |
| 6604 | ret = mtd_ooblayout_count_freebytes(mtd); |
| 6605 | if (ret < 0) |
| 6606 | ret = 0; |
| 6607 | |
| 6608 | mtd->oobavail = ret; |
| 6609 | |
| 6610 | /* ECC sanity check: warn if it's too weak */ |
| 6611 | if (!nand_ecc_strength_good(mtd)) |
| 6612 | pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n", |
| 6613 | mtd->name); |
| 6614 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 6615 | /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */ |
Huang Shijie | 1d0ed69 | 2013-09-25 14:58:10 +0800 | [diff] [blame] | 6616 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) { |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 6617 | switch (ecc->steps) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 6618 | case 2: |
| 6619 | mtd->subpage_sft = 1; |
| 6620 | break; |
| 6621 | case 4: |
| 6622 | case 8: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 6623 | case 16: |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 6624 | mtd->subpage_sft = 2; |
| 6625 | break; |
| 6626 | } |
| 6627 | } |
| 6628 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; |
| 6629 | |
Thomas Gleixner | 04bbd0e | 2006-05-25 09:45:29 +0200 | [diff] [blame] | 6630 | /* Initialize state */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 6631 | chip->state = FL_READY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6633 | /* Invalidate the pagebuffer reference */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 6634 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6635 | |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 6636 | /* Large page NAND with SOFT_ECC should support subpage reads */ |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 6637 | switch (ecc->mode) { |
| 6638 | case NAND_ECC_SOFT: |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 6639 | if (chip->page_shift > 9) |
| 6640 | chip->options |= NAND_SUBPAGE_READ; |
| 6641 | break; |
| 6642 | |
| 6643 | default: |
| 6644 | break; |
| 6645 | } |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 6646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6647 | /* Fill in remaining MTD driver data */ |
Huang Shijie | 963d1c2 | 2013-09-25 14:58:21 +0800 | [diff] [blame] | 6648 | mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH; |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 6649 | mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM : |
| 6650 | MTD_CAP_NANDFLASH; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 6651 | mtd->_erase = nand_erase; |
| 6652 | mtd->_point = NULL; |
| 6653 | mtd->_unpoint = NULL; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 6654 | mtd->_panic_write = panic_nand_write; |
| 6655 | mtd->_read_oob = nand_read_oob; |
| 6656 | mtd->_write_oob = nand_write_oob; |
| 6657 | mtd->_sync = nand_sync; |
| 6658 | mtd->_lock = NULL; |
| 6659 | mtd->_unlock = NULL; |
| 6660 | mtd->_suspend = nand_suspend; |
| 6661 | mtd->_resume = nand_resume; |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 6662 | mtd->_reboot = nand_shutdown; |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 6663 | mtd->_block_isreserved = nand_block_isreserved; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 6664 | mtd->_block_isbad = nand_block_isbad; |
| 6665 | mtd->_block_markbad = nand_block_markbad; |
Zach Brown | 5671842 | 2017-01-10 13:30:20 -0600 | [diff] [blame] | 6666 | mtd->_max_bad_blocks = nand_max_bad_blocks; |
Anatolij Gustschin | cbcab65 | 2010-12-16 23:42:16 +0100 | [diff] [blame] | 6667 | mtd->writebufsize = mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6668 | |
Shmulik Ladkani | ea3b2ea | 2012-06-08 18:29:06 +0300 | [diff] [blame] | 6669 | /* |
| 6670 | * Initialize bitflip_threshold to its default prior scan_bbt() call. |
| 6671 | * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be |
| 6672 | * properly set. |
| 6673 | */ |
| 6674 | if (!mtd->bitflip_threshold) |
Brian Norris | 240181f | 2015-01-12 12:51:29 -0800 | [diff] [blame] | 6675 | mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6676 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6677 | /* Initialize the ->data_interface field. */ |
| 6678 | ret = nand_init_data_interface(chip); |
| 6679 | if (ret) |
| 6680 | goto err_nand_manuf_cleanup; |
| 6681 | |
| 6682 | /* Enter fastest possible mode on all dies. */ |
| 6683 | for (i = 0; i < chip->numchips; i++) { |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6684 | ret = nand_setup_data_interface(chip, i); |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6685 | if (ret) |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 6686 | goto err_nand_manuf_cleanup; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6687 | } |
| 6688 | |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 6689 | /* Check, if we should skip the bad block table scan */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 6690 | if (chip->options & NAND_SKIP_BBTSCAN) |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 6691 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6692 | |
| 6693 | /* Build bad block table */ |
Boris Brezillon | e80eba7 | 2018-07-05 12:27:31 +0200 | [diff] [blame] | 6694 | ret = nand_create_bbt(chip); |
Brian Norris | 44d4182 | 2017-05-01 17:04:50 -0700 | [diff] [blame] | 6695 | if (ret) |
Miquel Raynal | 17fa804 | 2017-11-30 18:01:31 +0100 | [diff] [blame] | 6696 | goto err_nand_manuf_cleanup; |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6697 | |
Brian Norris | 44d4182 | 2017-05-01 17:04:50 -0700 | [diff] [blame] | 6698 | return 0; |
| 6699 | |
Boris Brezillon | f84674b | 2017-06-02 12:18:24 +0200 | [diff] [blame] | 6700 | |
| 6701 | err_nand_manuf_cleanup: |
| 6702 | nand_manufacturer_cleanup(chip); |
| 6703 | |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 6704 | err_free_buf: |
| 6705 | kfree(chip->data_buf); |
| 6706 | kfree(ecc->code_buf); |
| 6707 | kfree(ecc->calc_buf); |
Brian Norris | 7877104 | 2017-05-01 17:04:53 -0700 | [diff] [blame] | 6708 | |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 6709 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6710 | } |
| 6711 | |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 6712 | static int nand_attach(struct nand_chip *chip) |
| 6713 | { |
| 6714 | if (chip->controller->ops && chip->controller->ops->attach_chip) |
| 6715 | return chip->controller->ops->attach_chip(chip); |
| 6716 | |
| 6717 | return 0; |
| 6718 | } |
| 6719 | |
| 6720 | static void nand_detach(struct nand_chip *chip) |
| 6721 | { |
| 6722 | if (chip->controller->ops && chip->controller->ops->detach_chip) |
| 6723 | chip->controller->ops->detach_chip(chip); |
| 6724 | } |
| 6725 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 6726 | /** |
Miquel Raynal | 256c4fc | 2018-04-22 18:02:30 +0200 | [diff] [blame] | 6727 | * nand_scan_with_ids - [NAND Interface] Scan for the NAND device |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 6728 | * @chip: NAND chip object |
Boris Brezillon | 800342d | 2018-08-04 22:59:23 +0200 | [diff] [blame] | 6729 | * @maxchips: number of chips to scan for. |
Miquel Raynal | 256c4fc | 2018-04-22 18:02:30 +0200 | [diff] [blame] | 6730 | * @ids: optional flash IDs table |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 6731 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 6732 | * This fills out all the uninitialized function pointers with the defaults. |
| 6733 | * 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] | 6734 | * appropriate values. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 6735 | */ |
Boris Brezillon | 871a407 | 2018-08-04 22:59:22 +0200 | [diff] [blame] | 6736 | int nand_scan_with_ids(struct nand_chip *chip, unsigned int maxchips, |
Miquel Raynal | 256c4fc | 2018-04-22 18:02:30 +0200 | [diff] [blame] | 6737 | struct nand_flash_dev *ids) |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 6738 | { |
| 6739 | int ret; |
| 6740 | |
Boris Brezillon | 800342d | 2018-08-04 22:59:23 +0200 | [diff] [blame] | 6741 | if (!maxchips) |
| 6742 | return -EINVAL; |
| 6743 | |
| 6744 | ret = nand_scan_ident(chip, maxchips, ids); |
| 6745 | if (ret) |
| 6746 | return ret; |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 6747 | |
| 6748 | ret = nand_attach(chip); |
| 6749 | if (ret) |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 6750 | goto cleanup_ident; |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 6751 | |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 6752 | ret = nand_scan_tail(chip); |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 6753 | if (ret) |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 6754 | goto detach_chip; |
| 6755 | |
| 6756 | return 0; |
| 6757 | |
| 6758 | detach_chip: |
| 6759 | nand_detach(chip); |
| 6760 | cleanup_ident: |
| 6761 | nand_scan_ident_cleanup(chip); |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 6762 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 6763 | return ret; |
| 6764 | } |
Miquel Raynal | 256c4fc | 2018-04-22 18:02:30 +0200 | [diff] [blame] | 6765 | EXPORT_SYMBOL(nand_scan_with_ids); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 6766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6767 | /** |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 6768 | * nand_cleanup - [NAND Interface] Free resources held by the NAND device |
| 6769 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 6770 | */ |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 6771 | void nand_cleanup(struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6772 | { |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 6773 | if (chip->ecc.mode == NAND_ECC_SOFT && |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 6774 | chip->ecc.algo == NAND_ECC_BCH) |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 6775 | nand_bch_free((struct nand_bch_control *)chip->ecc.priv); |
| 6776 | |
Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 6777 | /* Free bad block table memory */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 6778 | kfree(chip->bbt); |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 6779 | kfree(chip->data_buf); |
| 6780 | kfree(chip->ecc.code_buf); |
| 6781 | kfree(chip->ecc.calc_buf); |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 6782 | |
| 6783 | /* Free bad block descriptor memory */ |
| 6784 | if (chip->badblock_pattern && chip->badblock_pattern->options |
| 6785 | & NAND_BBT_DYNAMICSTRUCT) |
| 6786 | kfree(chip->badblock_pattern); |
Boris Brezillon | abbe26d | 2016-06-08 09:32:55 +0200 | [diff] [blame] | 6787 | |
| 6788 | /* Free manufacturer priv data. */ |
| 6789 | nand_manufacturer_cleanup(chip); |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 6790 | |
| 6791 | /* Free controller specific allocations after chip identification */ |
| 6792 | nand_detach(chip); |
Miquel Raynal | 2023f1fa | 2018-07-25 15:31:51 +0200 | [diff] [blame] | 6793 | |
| 6794 | /* Free identification phase allocations */ |
| 6795 | nand_scan_ident_cleanup(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6796 | } |
Miquel Raynal | 05b54c7 | 2018-07-19 01:05:46 +0200 | [diff] [blame] | 6797 | |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 6798 | EXPORT_SYMBOL_GPL(nand_cleanup); |
| 6799 | |
| 6800 | /** |
| 6801 | * nand_release - [NAND Interface] Unregister the MTD device and free resources |
| 6802 | * held by the NAND device |
Boris Brezillon | 59ac276 | 2018-09-06 14:05:15 +0200 | [diff] [blame] | 6803 | * @chip: NAND chip object |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 6804 | */ |
Boris Brezillon | 59ac276 | 2018-09-06 14:05:15 +0200 | [diff] [blame] | 6805 | void nand_release(struct nand_chip *chip) |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 6806 | { |
Boris Brezillon | 59ac276 | 2018-09-06 14:05:15 +0200 | [diff] [blame] | 6807 | mtd_device_unregister(nand_to_mtd(chip)); |
| 6808 | nand_cleanup(chip); |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 6809 | } |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 6810 | EXPORT_SYMBOL_GPL(nand_release); |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 6811 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 6812 | MODULE_LICENSE("GPL"); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 6813 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>"); |
| 6814 | MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>"); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 6815 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |