Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Overview: |
| 3 | * This is the generic MTD driver for NAND flash devices. It should be |
| 4 | * capable of working with almost all NAND chips currently available. |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Additional technical information is available on |
maximilian attems | 8b2b403 | 2007-07-28 13:07:16 +0200 | [diff] [blame] | 7 | * http://www.linux-mtd.infradead.org/doc/nand.html |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 10 | * 2002-2006 Thomas Gleixner (tglx@linutronix.de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 12 | * Credits: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 13 | * David Woodhouse for adding multichip support |
| 14 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * Aleph One Ltd. and Toby Churchill Ltd. for supporting the |
| 16 | * rework for 2K page size chips |
| 17 | * |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 18 | * TODO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | * Enable cached programming for 2k page size chips |
| 20 | * Check, if mtd->ecctype should be set to MTD_ECC_HW |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 21 | * if we have HW ECC support. |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 22 | * BBT table is not serialized, has to be fixed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * This program is free software; you can redistribute it and/or modify |
| 25 | * it under the terms of the GNU General Public License version 2 as |
| 26 | * published by the Free Software Foundation. |
| 27 | * |
| 28 | */ |
| 29 | |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 30 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 31 | |
David Woodhouse | 552d920 | 2006-05-14 01:20:46 +0100 | [diff] [blame] | 32 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/delay.h> |
| 34 | #include <linux/errno.h> |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 35 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/sched.h> |
| 37 | #include <linux/slab.h> |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 38 | #include <linux/mm.h> |
Ingo Molnar | 38b8d20 | 2017-02-08 18:51:31 +0100 | [diff] [blame] | 39 | #include <linux/nmi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/types.h> |
| 41 | #include <linux/mtd/mtd.h> |
| 42 | #include <linux/mtd/nand.h> |
| 43 | #include <linux/mtd/nand_ecc.h> |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 44 | #include <linux/mtd/nand_bch.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/interrupt.h> |
| 46 | #include <linux/bitops.h> |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 47 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/mtd/partitions.h> |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 49 | #include <linux/of.h> |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 50 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 51 | static int nand_get_device(struct mtd_info *mtd, int new_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 53 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 54 | struct mtd_oob_ops *ops); |
| 55 | |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 56 | /* Define default oob placement schemes for large and small page devices */ |
| 57 | static int nand_ooblayout_ecc_sp(struct mtd_info *mtd, int section, |
| 58 | struct mtd_oob_region *oobregion) |
| 59 | { |
| 60 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 61 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 62 | |
| 63 | if (section > 1) |
| 64 | return -ERANGE; |
| 65 | |
| 66 | if (!section) { |
| 67 | oobregion->offset = 0; |
| 68 | oobregion->length = 4; |
| 69 | } else { |
| 70 | oobregion->offset = 6; |
| 71 | oobregion->length = ecc->total - 4; |
| 72 | } |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static int nand_ooblayout_free_sp(struct mtd_info *mtd, int section, |
| 78 | struct mtd_oob_region *oobregion) |
| 79 | { |
| 80 | if (section > 1) |
| 81 | return -ERANGE; |
| 82 | |
| 83 | if (mtd->oobsize == 16) { |
| 84 | if (section) |
| 85 | return -ERANGE; |
| 86 | |
| 87 | oobregion->length = 8; |
| 88 | oobregion->offset = 8; |
| 89 | } else { |
| 90 | oobregion->length = 2; |
| 91 | if (!section) |
| 92 | oobregion->offset = 3; |
| 93 | else |
| 94 | oobregion->offset = 6; |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | const struct mtd_ooblayout_ops nand_ooblayout_sp_ops = { |
| 101 | .ecc = nand_ooblayout_ecc_sp, |
| 102 | .free = nand_ooblayout_free_sp, |
| 103 | }; |
| 104 | EXPORT_SYMBOL_GPL(nand_ooblayout_sp_ops); |
| 105 | |
| 106 | static int nand_ooblayout_ecc_lp(struct mtd_info *mtd, int section, |
| 107 | struct mtd_oob_region *oobregion) |
| 108 | { |
| 109 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 110 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 111 | |
| 112 | if (section) |
| 113 | return -ERANGE; |
| 114 | |
| 115 | oobregion->length = ecc->total; |
| 116 | oobregion->offset = mtd->oobsize - oobregion->length; |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static int nand_ooblayout_free_lp(struct mtd_info *mtd, int section, |
| 122 | struct mtd_oob_region *oobregion) |
| 123 | { |
| 124 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 125 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 126 | |
| 127 | if (section) |
| 128 | return -ERANGE; |
| 129 | |
| 130 | oobregion->length = mtd->oobsize - ecc->total - 2; |
| 131 | oobregion->offset = 2; |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | const struct mtd_ooblayout_ops nand_ooblayout_lp_ops = { |
| 137 | .ecc = nand_ooblayout_ecc_lp, |
| 138 | .free = nand_ooblayout_free_lp, |
| 139 | }; |
| 140 | EXPORT_SYMBOL_GPL(nand_ooblayout_lp_ops); |
Thomas Gleixner | d470a97 | 2006-05-23 23:48:57 +0200 | [diff] [blame] | 141 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 142 | static int check_offs_len(struct mtd_info *mtd, |
| 143 | loff_t ofs, uint64_t len) |
| 144 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 145 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 146 | int ret = 0; |
| 147 | |
| 148 | /* Start address must align on block boundary */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 149 | if (ofs & ((1ULL << chip->phys_erase_shift) - 1)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 150 | pr_debug("%s: unaligned address\n", __func__); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 151 | ret = -EINVAL; |
| 152 | } |
| 153 | |
| 154 | /* Length must align on block boundary */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 155 | if (len & ((1ULL << chip->phys_erase_shift) - 1)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 156 | pr_debug("%s: length not block aligned\n", __func__); |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 157 | ret = -EINVAL; |
| 158 | } |
| 159 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 160 | return ret; |
| 161 | } |
| 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | /** |
| 164 | * nand_release_device - [GENERIC] release chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 165 | * @mtd: MTD device structure |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 166 | * |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 167 | * Release chip lock and wake up anyone waiting on the device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 169 | static void nand_release_device(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 171 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 173 | /* Release the controller and the chip */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 174 | spin_lock(&chip->controller->lock); |
| 175 | chip->controller->active = NULL; |
| 176 | chip->state = FL_READY; |
| 177 | wake_up(&chip->controller->wq); |
| 178 | spin_unlock(&chip->controller->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /** |
| 182 | * nand_read_byte - [DEFAULT] read one byte from the chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 183 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 185 | * Default read function for 8bit buswidth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 187 | static uint8_t nand_read_byte(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 189 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 190 | return readb(chip->IO_ADDR_R); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /** |
Masanari Iida | 064a769 | 2012-11-09 23:20:58 +0900 | [diff] [blame] | 194 | * nand_read_byte16 - [DEFAULT] read one byte endianness aware from the chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 195 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 197 | * Default read function for 16bit buswidth with endianness conversion. |
| 198 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 200 | static uint8_t nand_read_byte16(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 202 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 203 | return (uint8_t) cpu_to_le16(readw(chip->IO_ADDR_R)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | * nand_read_word - [DEFAULT] read one word from the chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 208 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 210 | * Default read function for 16bit buswidth without endianness conversion. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | */ |
| 212 | static u16 nand_read_word(struct mtd_info *mtd) |
| 213 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 214 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 215 | return readw(chip->IO_ADDR_R); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | * nand_select_chip - [DEFAULT] control CE line |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 220 | * @mtd: MTD device structure |
| 221 | * @chipnr: chipnumber to select, -1 for deselect |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | * |
| 223 | * Default select function for 1 chip devices. |
| 224 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 225 | static void nand_select_chip(struct mtd_info *mtd, int chipnr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 227 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 228 | |
| 229 | switch (chipnr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | case -1: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 231 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | break; |
| 233 | case 0: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | break; |
| 235 | |
| 236 | default: |
| 237 | BUG(); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /** |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 242 | * nand_write_byte - [DEFAULT] write single byte to chip |
| 243 | * @mtd: MTD device structure |
| 244 | * @byte: value to write |
| 245 | * |
| 246 | * Default function to write a byte to I/O[7:0] |
| 247 | */ |
| 248 | static void nand_write_byte(struct mtd_info *mtd, uint8_t byte) |
| 249 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 250 | struct nand_chip *chip = mtd_to_nand(mtd); |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 251 | |
| 252 | chip->write_buf(mtd, &byte, 1); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * nand_write_byte16 - [DEFAULT] write single byte to a chip with width 16 |
| 257 | * @mtd: MTD device structure |
| 258 | * @byte: value to write |
| 259 | * |
| 260 | * Default function to write a byte to I/O[7:0] on a 16-bit wide chip. |
| 261 | */ |
| 262 | static void nand_write_byte16(struct mtd_info *mtd, uint8_t byte) |
| 263 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 264 | struct nand_chip *chip = mtd_to_nand(mtd); |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 265 | uint16_t word = byte; |
| 266 | |
| 267 | /* |
| 268 | * It's not entirely clear what should happen to I/O[15:8] when writing |
| 269 | * a byte. The ONFi spec (Revision 3.1; 2012-09-19, Section 2.16) reads: |
| 270 | * |
| 271 | * When the host supports a 16-bit bus width, only data is |
| 272 | * transferred at the 16-bit width. All address and command line |
| 273 | * transfers shall use only the lower 8-bits of the data bus. During |
| 274 | * command transfers, the host may place any value on the upper |
| 275 | * 8-bits of the data bus. During address transfers, the host shall |
| 276 | * set the upper 8-bits of the data bus to 00h. |
| 277 | * |
| 278 | * One user of the write_byte callback is nand_onfi_set_features. The |
| 279 | * four parameters are specified to be written to I/O[7:0], but this is |
| 280 | * neither an address nor a command transfer. Let's assume a 0 on the |
| 281 | * upper I/O lines is OK. |
| 282 | */ |
| 283 | chip->write_buf(mtd, (uint8_t *)&word, 2); |
| 284 | } |
| 285 | |
| 286 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | * nand_write_buf - [DEFAULT] write buffer to chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 288 | * @mtd: MTD device structure |
| 289 | * @buf: data buffer |
| 290 | * @len: number of bytes to write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 292 | * Default write function for 8bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 294 | static void nand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 296 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Alexander Shiyan | 7641383 | 2013-04-13 09:32:13 +0400 | [diff] [blame] | 298 | iowrite8_rep(chip->IO_ADDR_W, buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 302 | * nand_read_buf - [DEFAULT] read chip data into buffer |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 303 | * @mtd: MTD device structure |
| 304 | * @buf: buffer to store date |
| 305 | * @len: number of bytes to read |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 307 | * Default read function for 8bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 309 | static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 311 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
Alexander Shiyan | 7641383 | 2013-04-13 09:32:13 +0400 | [diff] [blame] | 313 | ioread8_rep(chip->IO_ADDR_R, buf, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | * nand_write_buf16 - [DEFAULT] write buffer to chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 318 | * @mtd: MTD device structure |
| 319 | * @buf: data buffer |
| 320 | * @len: number of bytes to write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 322 | * Default write function for 16bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 324 | static void nand_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 326 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | u16 *p = (u16 *) buf; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 328 | |
Alexander Shiyan | 7641383 | 2013-04-13 09:32:13 +0400 | [diff] [blame] | 329 | iowrite16_rep(chip->IO_ADDR_W, p, len >> 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 333 | * nand_read_buf16 - [DEFAULT] read chip data into buffer |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 334 | * @mtd: MTD device structure |
| 335 | * @buf: buffer to store date |
| 336 | * @len: number of bytes to read |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 338 | * Default read function for 16bit buswidth. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | */ |
Thomas Gleixner | 58dd8f2b | 2006-05-23 11:52:35 +0200 | [diff] [blame] | 340 | static void nand_read_buf16(struct mtd_info *mtd, uint8_t *buf, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 342 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | u16 *p = (u16 *) buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
Alexander Shiyan | 7641383 | 2013-04-13 09:32:13 +0400 | [diff] [blame] | 345 | ioread16_rep(chip->IO_ADDR_R, p, len >> 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 350 | * @mtd: MTD device structure |
| 351 | * @ofs: offset from device start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 353 | * Check, if the block is bad. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | */ |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 355 | static int nand_block_bad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | { |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 357 | int page, res = 0, i = 0; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 358 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | u16 bad; |
| 360 | |
Brian Norris | 5fb1549 | 2011-05-31 16:31:21 -0700 | [diff] [blame] | 361 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
Kevin Cernekee | b60b08b | 2010-05-04 20:58:10 -0700 | [diff] [blame] | 362 | ofs += mtd->erasesize - mtd->writesize; |
| 363 | |
Thomas Knobloch | 1a12f46 | 2007-05-03 07:39:37 +0100 | [diff] [blame] | 364 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
| 365 | |
Brian Norris | cdbec05 | 2012-01-13 18:11:48 -0800 | [diff] [blame] | 366 | do { |
| 367 | if (chip->options & NAND_BUSWIDTH_16) { |
| 368 | chip->cmdfunc(mtd, NAND_CMD_READOOB, |
| 369 | chip->badblockpos & 0xFE, page); |
| 370 | bad = cpu_to_le16(chip->read_word(mtd)); |
| 371 | if (chip->badblockpos & 0x1) |
| 372 | bad >>= 8; |
| 373 | else |
| 374 | bad &= 0xFF; |
| 375 | } else { |
| 376 | chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, |
| 377 | page); |
| 378 | bad = chip->read_byte(mtd); |
| 379 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 380 | |
Brian Norris | cdbec05 | 2012-01-13 18:11:48 -0800 | [diff] [blame] | 381 | if (likely(chip->badblockbits == 8)) |
| 382 | res = bad != 0xFF; |
| 383 | else |
| 384 | res = hweight8(bad) < chip->badblockbits; |
| 385 | ofs += mtd->writesize; |
| 386 | page = (int)(ofs >> chip->page_shift) & chip->pagemask; |
| 387 | i++; |
| 388 | } while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE)); |
Maxim Levitsky | e0b58d0 | 2010-02-22 20:39:38 +0200 | [diff] [blame] | 389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | return res; |
| 391 | } |
| 392 | |
| 393 | /** |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 394 | * nand_default_block_markbad - [DEFAULT] mark a block bad via bad block marker |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 395 | * @mtd: MTD device structure |
| 396 | * @ofs: offset from device start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 398 | * This is the default implementation, which can be overridden by a hardware |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 399 | * specific driver. It provides the details for writing a bad block marker to a |
| 400 | * block. |
| 401 | */ |
| 402 | static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 403 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 404 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 405 | struct mtd_oob_ops ops; |
| 406 | uint8_t buf[2] = { 0, 0 }; |
| 407 | int ret = 0, res, i = 0; |
| 408 | |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 409 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 410 | ops.oobbuf = buf; |
| 411 | ops.ooboffs = chip->badblockpos; |
| 412 | if (chip->options & NAND_BUSWIDTH_16) { |
| 413 | ops.ooboffs &= ~0x01; |
| 414 | ops.len = ops.ooblen = 2; |
| 415 | } else { |
| 416 | ops.len = ops.ooblen = 1; |
| 417 | } |
| 418 | ops.mode = MTD_OPS_PLACE_OOB; |
| 419 | |
| 420 | /* Write to first/last page(s) if necessary */ |
| 421 | if (chip->bbt_options & NAND_BBT_SCANLASTPAGE) |
| 422 | ofs += mtd->erasesize - mtd->writesize; |
| 423 | do { |
| 424 | res = nand_do_write_oob(mtd, ofs, &ops); |
| 425 | if (!ret) |
| 426 | ret = res; |
| 427 | |
| 428 | i++; |
| 429 | ofs += mtd->writesize; |
| 430 | } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2); |
| 431 | |
| 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * nand_block_markbad_lowlevel - mark a block bad |
| 437 | * @mtd: MTD device structure |
| 438 | * @ofs: offset from device start |
| 439 | * |
| 440 | * This function performs the generic NAND bad block marking steps (i.e., bad |
| 441 | * block table(s) and/or marker(s)). We only allow the hardware driver to |
| 442 | * specify how to write bad block markers to OOB (chip->block_markbad). |
| 443 | * |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 444 | * We try operations in the following order: |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 445 | * (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] | 446 | * (2) write bad block marker to OOB area of affected block (unless flag |
| 447 | * NAND_BBT_NO_OOB_BBM is present) |
| 448 | * (3) update the BBT |
| 449 | * 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] | 450 | * procedures, and dump the error in the end. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | */ |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 452 | 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] | 453 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 454 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 455 | int res, ret = 0; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 456 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 457 | if (!(chip->bbt_options & NAND_BBT_NO_OOB_BBM)) { |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 458 | struct erase_info einfo; |
| 459 | |
| 460 | /* Attempt erase before marking OOB */ |
| 461 | memset(&einfo, 0, sizeof(einfo)); |
| 462 | einfo.mtd = mtd; |
| 463 | einfo.addr = ofs; |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 464 | einfo.len = 1ULL << chip->phys_erase_shift; |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 465 | nand_erase_nand(mtd, &einfo, 0); |
Brian Norris | 0091842 | 2012-01-13 18:11:47 -0800 | [diff] [blame] | 466 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 467 | /* Write bad block marker to OOB */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 468 | nand_get_device(mtd, FL_WRITING); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 469 | ret = chip->block_markbad(mtd, ofs); |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 470 | nand_release_device(mtd); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 471 | } |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 472 | |
Brian Norris | b32843b | 2013-07-30 17:52:59 -0700 | [diff] [blame] | 473 | /* Mark block bad in BBT */ |
| 474 | if (chip->bbt) { |
| 475 | res = nand_markbad_bbt(mtd, ofs); |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 476 | if (!ret) |
| 477 | ret = res; |
| 478 | } |
| 479 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 480 | if (!ret) |
| 481 | mtd->ecc_stats.badblocks++; |
Artem Bityutskiy | c0b8ba7 | 2007-07-23 16:06:50 +0300 | [diff] [blame] | 482 | |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 483 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 486 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | * nand_check_wp - [GENERIC] check if the chip is write protected |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 488 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 490 | * Check, if the device is write protected. The function expects, that the |
| 491 | * device is already selected. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 493 | static int nand_check_wp(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 495 | struct nand_chip *chip = mtd_to_nand(mtd); |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 496 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 497 | /* Broken xD cards report WP despite being writable */ |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 498 | if (chip->options & NAND_BROKEN_XD) |
| 499 | return 0; |
| 500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | /* Check the WP bit */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 502 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
| 503 | return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /** |
Gu Zheng | c30e1f7 | 2014-09-03 17:49:10 +0800 | [diff] [blame] | 507 | * nand_block_isreserved - [GENERIC] Check if a block is marked reserved. |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 508 | * @mtd: MTD device structure |
| 509 | * @ofs: offset from device start |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 510 | * |
Gu Zheng | c30e1f7 | 2014-09-03 17:49:10 +0800 | [diff] [blame] | 511 | * Check if the block is marked as reserved. |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 512 | */ |
| 513 | static int nand_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
| 514 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 515 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 516 | |
| 517 | if (!chip->bbt) |
| 518 | return 0; |
| 519 | /* Return info from the table */ |
| 520 | return nand_isreserved_bbt(mtd, ofs); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad |
| 525 | * @mtd: MTD device structure |
| 526 | * @ofs: offset from device start |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 527 | * @allowbbt: 1, if its allowed to access the bbt area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | * |
| 529 | * Check, if the block is bad. Either by reading the bad block table or |
| 530 | * calling of the scan function. |
| 531 | */ |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 532 | 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] | 533 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 534 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 535 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 536 | if (!chip->bbt) |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 537 | return chip->block_bad(mtd, ofs); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 538 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | /* Return info from the table */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 540 | return nand_isbad_bbt(mtd, ofs, allowbbt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 543 | /** |
| 544 | * panic_nand_wait_ready - [GENERIC] Wait for the ready pin after commands. |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 545 | * @mtd: MTD device structure |
| 546 | * @timeo: Timeout |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 547 | * |
| 548 | * Helper function for nand_wait_ready used when needing to wait in interrupt |
| 549 | * context. |
| 550 | */ |
| 551 | static void panic_nand_wait_ready(struct mtd_info *mtd, unsigned long timeo) |
| 552 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 553 | struct nand_chip *chip = mtd_to_nand(mtd); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 554 | int i; |
| 555 | |
| 556 | /* Wait for the device to get ready */ |
| 557 | for (i = 0; i < timeo; i++) { |
| 558 | if (chip->dev_ready(mtd)) |
| 559 | break; |
| 560 | touch_softlockup_watchdog(); |
| 561 | mdelay(1); |
| 562 | } |
| 563 | } |
| 564 | |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 565 | /** |
| 566 | * nand_wait_ready - [GENERIC] Wait for the ready pin after commands. |
| 567 | * @mtd: MTD device structure |
| 568 | * |
| 569 | * Wait for the ready pin after a command, and warn if a timeout occurs. |
| 570 | */ |
David Woodhouse | 4b648b0 | 2006-09-25 17:05:24 +0100 | [diff] [blame] | 571 | void nand_wait_ready(struct mtd_info *mtd) |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 572 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 573 | struct nand_chip *chip = mtd_to_nand(mtd); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 574 | unsigned long timeo = 400; |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 575 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 576 | if (in_interrupt() || oops_in_progress) |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 577 | return panic_nand_wait_ready(mtd, timeo); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 578 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 579 | /* Wait until command is processed or timeout occurs */ |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 580 | timeo = jiffies + msecs_to_jiffies(timeo); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 581 | do { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 582 | if (chip->dev_ready(mtd)) |
Ezequiel Garcia | 4c7e054 | 2016-04-12 17:46:41 -0300 | [diff] [blame] | 583 | return; |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 584 | cond_resched(); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 585 | } while (time_before(jiffies, timeo)); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 586 | |
Brian Norris | 9ebfdf5 | 2016-03-04 17:19:23 -0800 | [diff] [blame] | 587 | if (!chip->dev_ready(mtd)) |
| 588 | pr_warn_ratelimited("timeout while waiting for chip to become ready\n"); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 589 | } |
David Woodhouse | 4b648b0 | 2006-09-25 17:05:24 +0100 | [diff] [blame] | 590 | EXPORT_SYMBOL_GPL(nand_wait_ready); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | /** |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 593 | * nand_wait_status_ready - [GENERIC] Wait for the ready status after commands. |
| 594 | * @mtd: MTD device structure |
| 595 | * @timeo: Timeout in ms |
| 596 | * |
| 597 | * Wait for status ready (i.e. command done) or timeout. |
| 598 | */ |
| 599 | static void nand_wait_status_ready(struct mtd_info *mtd, unsigned long timeo) |
| 600 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 601 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 602 | |
| 603 | timeo = jiffies + msecs_to_jiffies(timeo); |
| 604 | do { |
| 605 | if ((chip->read_byte(mtd) & NAND_STATUS_READY)) |
| 606 | break; |
| 607 | touch_softlockup_watchdog(); |
| 608 | } while (time_before(jiffies, timeo)); |
| 609 | }; |
| 610 | |
| 611 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | * nand_command - [DEFAULT] Send command to NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 613 | * @mtd: MTD device structure |
| 614 | * @command: the command to be sent |
| 615 | * @column: the column address for this command, -1 if none |
| 616 | * @page_addr: the page address for this command, -1 if none |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 618 | * 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] | 619 | * (512 Bytes per page). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 621 | static void nand_command(struct mtd_info *mtd, unsigned int command, |
| 622 | int column, int page_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 624 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 625 | int ctrl = NAND_CTRL_CLE | NAND_CTRL_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 627 | /* Write out the command to the device */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | if (command == NAND_CMD_SEQIN) { |
| 629 | int readcmd; |
| 630 | |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 631 | if (column >= mtd->writesize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | /* OOB area */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 633 | column -= mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | readcmd = NAND_CMD_READOOB; |
| 635 | } else if (column < 256) { |
| 636 | /* First 256 bytes --> READ0 */ |
| 637 | readcmd = NAND_CMD_READ0; |
| 638 | } else { |
| 639 | column -= 256; |
| 640 | readcmd = NAND_CMD_READ1; |
| 641 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 642 | chip->cmd_ctrl(mtd, readcmd, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 643 | ctrl &= ~NAND_CTRL_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 645 | chip->cmd_ctrl(mtd, command, ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 647 | /* Address cycle, when necessary */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 648 | ctrl = NAND_CTRL_ALE | NAND_CTRL_CHANGE; |
| 649 | /* Serially input address */ |
| 650 | if (column != -1) { |
| 651 | /* Adjust columns for 16 bit buswidth */ |
Brian Norris | 3dad234 | 2014-01-29 14:08:12 -0800 | [diff] [blame] | 652 | if (chip->options & NAND_BUSWIDTH_16 && |
| 653 | !nand_opcode_8bits(command)) |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 654 | column >>= 1; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 655 | chip->cmd_ctrl(mtd, column, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 656 | ctrl &= ~NAND_CTRL_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | } |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 658 | if (page_addr != -1) { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 659 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 660 | ctrl &= ~NAND_CTRL_CHANGE; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 661 | chip->cmd_ctrl(mtd, page_addr >> 8, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 662 | /* One more address cycle for devices > 32MiB */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 663 | if (chip->chipsize > (32 << 20)) |
| 664 | chip->cmd_ctrl(mtd, page_addr >> 16, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 665 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 666 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 667 | |
| 668 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 669 | * Program and erase have their own busy handlers status and sequential |
| 670 | * in needs no delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 671 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | switch (command) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | case NAND_CMD_PAGEPROG: |
| 675 | case NAND_CMD_ERASE1: |
| 676 | case NAND_CMD_ERASE2: |
| 677 | case NAND_CMD_SEQIN: |
| 678 | case NAND_CMD_STATUS: |
| 679 | return; |
| 680 | |
| 681 | case NAND_CMD_RESET: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 682 | if (chip->dev_ready) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | break; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 684 | udelay(chip->chip_delay); |
| 685 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 686 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 12efdde | 2006-05-24 22:57:09 +0200 | [diff] [blame] | 687 | chip->cmd_ctrl(mtd, |
| 688 | NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 689 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 690 | nand_wait_status_ready(mtd, 250); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | return; |
| 692 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 693 | /* This applies to read commands */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | default: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 695 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | * If we don't have access to the busy pin, we apply the given |
| 697 | * command delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 698 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 699 | if (!chip->dev_ready) { |
| 700 | udelay(chip->chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | return; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 702 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 704 | /* |
| 705 | * Apply this short delay always to ensure that we do wait tWB in |
| 706 | * any case on any machine. |
| 707 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 708 | ndelay(100); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 709 | |
| 710 | nand_wait_ready(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 713 | static void nand_ccs_delay(struct nand_chip *chip) |
| 714 | { |
| 715 | /* |
| 716 | * The controller already takes care of waiting for tCCS when the RNDIN |
| 717 | * or RNDOUT command is sent, return directly. |
| 718 | */ |
| 719 | if (!(chip->options & NAND_WAIT_TCCS)) |
| 720 | return; |
| 721 | |
| 722 | /* |
| 723 | * Wait tCCS_min if it is correctly defined, otherwise wait 500ns |
| 724 | * (which should be safe for all NANDs). |
| 725 | */ |
| 726 | if (chip->data_interface && chip->data_interface->timings.sdr.tCCS_min) |
| 727 | ndelay(chip->data_interface->timings.sdr.tCCS_min / 1000); |
| 728 | else |
| 729 | ndelay(500); |
| 730 | } |
| 731 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | /** |
| 733 | * nand_command_lp - [DEFAULT] Send command to NAND large page device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 734 | * @mtd: MTD device structure |
| 735 | * @command: the command to be sent |
| 736 | * @column: the column address for this command, -1 if none |
| 737 | * @page_addr: the page address for this command, -1 if none |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | * |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 739 | * 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] | 740 | * devices. We don't have the separate regions as we have in the small page |
| 741 | * devices. We must emulate NAND_CMD_READOOB to keep the code compatible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 743 | static void nand_command_lp(struct mtd_info *mtd, unsigned int command, |
| 744 | int column, int page_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 746 | register struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | |
| 748 | /* Emulate NAND_CMD_READOOB */ |
| 749 | if (command == NAND_CMD_READOOB) { |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 750 | column += mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | command = NAND_CMD_READ0; |
| 752 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 753 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 754 | /* Command latch cycle */ |
Alexander Shiyan | fb066ad | 2013-02-28 12:02:19 +0400 | [diff] [blame] | 755 | chip->cmd_ctrl(mtd, command, NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | |
| 757 | if (column != -1 || page_addr != -1) { |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 758 | int ctrl = NAND_CTRL_CHANGE | NAND_NCE | NAND_ALE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
| 760 | /* Serially input address */ |
| 761 | if (column != -1) { |
| 762 | /* Adjust columns for 16 bit buswidth */ |
Brian Norris | 3dad234 | 2014-01-29 14:08:12 -0800 | [diff] [blame] | 763 | if (chip->options & NAND_BUSWIDTH_16 && |
| 764 | !nand_opcode_8bits(command)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | column >>= 1; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 766 | chip->cmd_ctrl(mtd, column, ctrl); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 767 | ctrl &= ~NAND_CTRL_CHANGE; |
Boris Brezillon | fde85cf | 2016-06-15 13:09:51 +0200 | [diff] [blame] | 768 | |
Brian Norris | f5b88de | 2016-10-03 09:49:35 -0700 | [diff] [blame] | 769 | /* Only output a single addr cycle for 8bits opcodes. */ |
Boris Brezillon | fde85cf | 2016-06-15 13:09:51 +0200 | [diff] [blame] | 770 | if (!nand_opcode_8bits(command)) |
| 771 | chip->cmd_ctrl(mtd, column >> 8, ctrl); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 772 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | if (page_addr != -1) { |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 774 | chip->cmd_ctrl(mtd, page_addr, ctrl); |
| 775 | chip->cmd_ctrl(mtd, page_addr >> 8, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 776 | NAND_NCE | NAND_ALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | /* One more address cycle for devices > 128MiB */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 778 | if (chip->chipsize > (128 << 20)) |
| 779 | chip->cmd_ctrl(mtd, page_addr >> 16, |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 780 | NAND_NCE | NAND_ALE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | } |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 783 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 784 | |
| 785 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 786 | * Program and erase have their own busy handlers status, sequential |
Gerhard Sittig | 7a442f1 | 2014-03-29 14:36:22 +0100 | [diff] [blame] | 787 | * in and status need no delay. |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 788 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | switch (command) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 790 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | case NAND_CMD_CACHEDPROG: |
| 792 | case NAND_CMD_PAGEPROG: |
| 793 | case NAND_CMD_ERASE1: |
| 794 | case NAND_CMD_ERASE2: |
| 795 | case NAND_CMD_SEQIN: |
| 796 | case NAND_CMD_STATUS: |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 797 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 799 | case NAND_CMD_RNDIN: |
| 800 | nand_ccs_delay(chip); |
| 801 | return; |
| 802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | case NAND_CMD_RESET: |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 804 | if (chip->dev_ready) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | break; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 806 | udelay(chip->chip_delay); |
Thomas Gleixner | 12efdde | 2006-05-24 22:57:09 +0200 | [diff] [blame] | 807 | chip->cmd_ctrl(mtd, NAND_CMD_STATUS, |
| 808 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 809 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 810 | NAND_NCE | NAND_CTRL_CHANGE); |
Roger Quadros | 60c70d6 | 2015-02-23 17:26:39 +0200 | [diff] [blame] | 811 | /* EZ-NAND can take upto 250ms as per ONFi v4.0 */ |
| 812 | nand_wait_status_ready(mtd, 250); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | return; |
| 814 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 815 | case NAND_CMD_RNDOUT: |
| 816 | /* No ready / busy check necessary */ |
| 817 | chip->cmd_ctrl(mtd, NAND_CMD_RNDOUTSTART, |
| 818 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 819 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 820 | NAND_NCE | NAND_CTRL_CHANGE); |
Boris Brezillon | 6ea40a3 | 2016-10-01 10:24:03 +0200 | [diff] [blame] | 821 | |
| 822 | nand_ccs_delay(chip); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 823 | return; |
| 824 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | case NAND_CMD_READ0: |
Thomas Gleixner | 12efdde | 2006-05-24 22:57:09 +0200 | [diff] [blame] | 826 | chip->cmd_ctrl(mtd, NAND_CMD_READSTART, |
| 827 | NAND_NCE | NAND_CLE | NAND_CTRL_CHANGE); |
| 828 | chip->cmd_ctrl(mtd, NAND_CMD_NONE, |
| 829 | NAND_NCE | NAND_CTRL_CHANGE); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 830 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 831 | /* This applies to read commands */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | default: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 833 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | * 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] | 835 | * command delay. |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 836 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 837 | if (!chip->dev_ready) { |
| 838 | udelay(chip->chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | return; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 840 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | } |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 842 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 843 | /* |
| 844 | * Apply this short delay always to ensure that we do wait tWB in |
| 845 | * any case on any machine. |
| 846 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 847 | ndelay(100); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 848 | |
| 849 | nand_wait_ready(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 853 | * panic_nand_get_device - [GENERIC] Get chip for selected access |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 854 | * @chip: the nand chip descriptor |
| 855 | * @mtd: MTD device structure |
| 856 | * @new_state: the state which is requested |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 857 | * |
| 858 | * Used when in panic, no locks are taken. |
| 859 | */ |
| 860 | static void panic_nand_get_device(struct nand_chip *chip, |
| 861 | struct mtd_info *mtd, int new_state) |
| 862 | { |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 863 | /* Hardware controller shared among independent devices */ |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 864 | chip->controller->active = chip; |
| 865 | chip->state = new_state; |
| 866 | } |
| 867 | |
| 868 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | * nand_get_device - [GENERIC] Get chip for selected access |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 870 | * @mtd: MTD device structure |
| 871 | * @new_state: the state which is requested |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | * |
| 873 | * Get the device and lock it for exclusive access |
| 874 | */ |
Thomas Gleixner | 2c0a2be | 2006-05-23 11:50:56 +0200 | [diff] [blame] | 875 | static int |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 876 | nand_get_device(struct mtd_info *mtd, int new_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 878 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 879 | spinlock_t *lock = &chip->controller->lock; |
| 880 | wait_queue_head_t *wq = &chip->controller->wq; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 881 | DECLARE_WAITQUEUE(wait, current); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 882 | retry: |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 883 | spin_lock(lock); |
| 884 | |
vimal singh | b8b3ee9 | 2009-07-09 20:41:22 +0530 | [diff] [blame] | 885 | /* Hardware controller shared among independent devices */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 886 | if (!chip->controller->active) |
| 887 | chip->controller->active = chip; |
Thomas Gleixner | a36ed29 | 2006-05-23 11:37:03 +0200 | [diff] [blame] | 888 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 889 | if (chip->controller->active == chip && chip->state == FL_READY) { |
| 890 | chip->state = new_state; |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 891 | spin_unlock(lock); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 892 | return 0; |
| 893 | } |
| 894 | if (new_state == FL_PM_SUSPENDED) { |
Li Yang | 6b0d9a8 | 2009-11-17 14:45:49 -0800 | [diff] [blame] | 895 | if (chip->controller->active->state == FL_PM_SUSPENDED) { |
| 896 | chip->state = FL_PM_SUSPENDED; |
| 897 | spin_unlock(lock); |
| 898 | return 0; |
Li Yang | 6b0d9a8 | 2009-11-17 14:45:49 -0800 | [diff] [blame] | 899 | } |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 900 | } |
| 901 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 902 | add_wait_queue(wq, &wait); |
| 903 | spin_unlock(lock); |
| 904 | schedule(); |
| 905 | remove_wait_queue(wq, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | goto retry; |
| 907 | } |
| 908 | |
| 909 | /** |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 910 | * panic_nand_wait - [GENERIC] wait until the command is done |
| 911 | * @mtd: MTD device structure |
| 912 | * @chip: NAND chip structure |
| 913 | * @timeo: timeout |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 914 | * |
| 915 | * Wait for command done. This is a helper function for nand_wait used when |
| 916 | * 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] | 917 | * an oops through mtdoops. |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 918 | */ |
| 919 | static void panic_nand_wait(struct mtd_info *mtd, struct nand_chip *chip, |
| 920 | unsigned long timeo) |
| 921 | { |
| 922 | int i; |
| 923 | for (i = 0; i < timeo; i++) { |
| 924 | if (chip->dev_ready) { |
| 925 | if (chip->dev_ready(mtd)) |
| 926 | break; |
| 927 | } else { |
| 928 | if (chip->read_byte(mtd) & NAND_STATUS_READY) |
| 929 | break; |
| 930 | } |
| 931 | mdelay(1); |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 932 | } |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | /** |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 936 | * nand_wait - [DEFAULT] wait until the command is done |
| 937 | * @mtd: MTD device structure |
| 938 | * @chip: NAND chip structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | * |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 940 | * Wait for command done. This applies to erase and program only. |
Randy Dunlap | 844d3b4 | 2006-06-28 21:48:27 -0700 | [diff] [blame] | 941 | */ |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 942 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | { |
| 944 | |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 945 | int status; |
| 946 | unsigned long timeo = 400; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 948 | /* |
| 949 | * Apply this short delay always to ensure that we do wait tWB in any |
| 950 | * case on any machine. |
| 951 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 952 | ndelay(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | |
Artem Bityutskiy | 14c6578 | 2013-03-04 14:21:34 +0200 | [diff] [blame] | 954 | chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 956 | if (in_interrupt() || oops_in_progress) |
| 957 | panic_nand_wait(mtd, chip, timeo); |
| 958 | else { |
Huang Shijie | 6d2559f | 2013-01-30 10:03:56 +0800 | [diff] [blame] | 959 | timeo = jiffies + msecs_to_jiffies(timeo); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 960 | do { |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 961 | if (chip->dev_ready) { |
| 962 | if (chip->dev_ready(mtd)) |
| 963 | break; |
| 964 | } else { |
| 965 | if (chip->read_byte(mtd) & NAND_STATUS_READY) |
| 966 | break; |
| 967 | } |
| 968 | cond_resched(); |
Alex Smith | b70af9b | 2015-10-06 14:52:07 +0100 | [diff] [blame] | 969 | } while (time_before(jiffies, timeo)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | } |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 971 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 972 | status = (int)chip->read_byte(mtd); |
Matthieu CASTET | f251b8d | 2012-11-05 15:00:44 +0100 | [diff] [blame] | 973 | /* This can happen if in case of timeout or buggy dev_ready */ |
| 974 | WARN_ON(!(status & NAND_STATUS_READY)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | return status; |
| 976 | } |
| 977 | |
| 978 | /** |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 979 | * nand_reset_data_interface - Reset data interface and timings |
| 980 | * @chip: The NAND chip |
| 981 | * |
| 982 | * Reset the Data interface and timings to ONFI mode 0. |
| 983 | * |
| 984 | * Returns 0 for success or negative error code otherwise. |
| 985 | */ |
| 986 | static int nand_reset_data_interface(struct nand_chip *chip) |
| 987 | { |
| 988 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 989 | const struct nand_data_interface *conf; |
| 990 | int ret; |
| 991 | |
| 992 | if (!chip->setup_data_interface) |
| 993 | return 0; |
| 994 | |
| 995 | /* |
| 996 | * The ONFI specification says: |
| 997 | * " |
| 998 | * To transition from NV-DDR or NV-DDR2 to the SDR data |
| 999 | * interface, the host shall use the Reset (FFh) command |
| 1000 | * using SDR timing mode 0. A device in any timing mode is |
| 1001 | * required to recognize Reset (FFh) command issued in SDR |
| 1002 | * timing mode 0. |
| 1003 | * " |
| 1004 | * |
| 1005 | * Configure the data interface in SDR mode and set the |
| 1006 | * timings to timing mode 0. |
| 1007 | */ |
| 1008 | |
| 1009 | conf = nand_get_default_data_interface(); |
| 1010 | ret = chip->setup_data_interface(mtd, conf, false); |
| 1011 | if (ret) |
| 1012 | pr_err("Failed to configure data interface to SDR timing mode 0\n"); |
| 1013 | |
| 1014 | return ret; |
| 1015 | } |
| 1016 | |
| 1017 | /** |
| 1018 | * nand_setup_data_interface - Setup the best data interface and timings |
| 1019 | * @chip: The NAND chip |
| 1020 | * |
| 1021 | * Find and configure the best data interface and NAND timings supported by |
| 1022 | * the chip and the driver. |
| 1023 | * First tries to retrieve supported timing modes from ONFI information, |
| 1024 | * and if the NAND chip does not support ONFI, relies on the |
| 1025 | * ->onfi_timing_mode_default specified in the nand_ids table. |
| 1026 | * |
| 1027 | * Returns 0 for success or negative error code otherwise. |
| 1028 | */ |
| 1029 | static int nand_setup_data_interface(struct nand_chip *chip) |
| 1030 | { |
| 1031 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1032 | int ret; |
| 1033 | |
| 1034 | if (!chip->setup_data_interface || !chip->data_interface) |
| 1035 | return 0; |
| 1036 | |
| 1037 | /* |
| 1038 | * Ensure the timing mode has been changed on the chip side |
| 1039 | * before changing timings on the controller side. |
| 1040 | */ |
| 1041 | if (chip->onfi_version) { |
| 1042 | u8 tmode_param[ONFI_SUBFEATURE_PARAM_LEN] = { |
| 1043 | chip->onfi_timing_mode_default, |
| 1044 | }; |
| 1045 | |
| 1046 | ret = chip->onfi_set_features(mtd, chip, |
| 1047 | ONFI_FEATURE_ADDR_TIMING_MODE, |
| 1048 | tmode_param); |
| 1049 | if (ret) |
| 1050 | goto err; |
| 1051 | } |
| 1052 | |
| 1053 | ret = chip->setup_data_interface(mtd, chip->data_interface, false); |
| 1054 | err: |
| 1055 | return ret; |
| 1056 | } |
| 1057 | |
| 1058 | /** |
| 1059 | * nand_init_data_interface - find the best data interface and timings |
| 1060 | * @chip: The NAND chip |
| 1061 | * |
| 1062 | * Find the best data interface and NAND timings supported by the chip |
| 1063 | * and the driver. |
| 1064 | * First tries to retrieve supported timing modes from ONFI information, |
| 1065 | * and if the NAND chip does not support ONFI, relies on the |
| 1066 | * ->onfi_timing_mode_default specified in the nand_ids table. After this |
| 1067 | * function nand_chip->data_interface is initialized with the best timing mode |
| 1068 | * available. |
| 1069 | * |
| 1070 | * Returns 0 for success or negative error code otherwise. |
| 1071 | */ |
| 1072 | static int nand_init_data_interface(struct nand_chip *chip) |
| 1073 | { |
| 1074 | struct mtd_info *mtd = nand_to_mtd(chip); |
| 1075 | int modes, mode, ret; |
| 1076 | |
| 1077 | if (!chip->setup_data_interface) |
| 1078 | return 0; |
| 1079 | |
| 1080 | /* |
| 1081 | * First try to identify the best timings from ONFI parameters and |
| 1082 | * if the NAND does not support ONFI, fallback to the default ONFI |
| 1083 | * timing mode. |
| 1084 | */ |
| 1085 | modes = onfi_get_async_timing_mode(chip); |
| 1086 | if (modes == ONFI_TIMING_MODE_UNKNOWN) { |
| 1087 | if (!chip->onfi_timing_mode_default) |
| 1088 | return 0; |
| 1089 | |
| 1090 | modes = GENMASK(chip->onfi_timing_mode_default, 0); |
| 1091 | } |
| 1092 | |
| 1093 | chip->data_interface = kzalloc(sizeof(*chip->data_interface), |
| 1094 | GFP_KERNEL); |
| 1095 | if (!chip->data_interface) |
| 1096 | return -ENOMEM; |
| 1097 | |
| 1098 | for (mode = fls(modes) - 1; mode >= 0; mode--) { |
| 1099 | ret = onfi_init_data_interface(chip, chip->data_interface, |
| 1100 | NAND_SDR_IFACE, mode); |
| 1101 | if (ret) |
| 1102 | continue; |
| 1103 | |
| 1104 | ret = chip->setup_data_interface(mtd, chip->data_interface, |
| 1105 | true); |
| 1106 | if (!ret) { |
| 1107 | chip->onfi_timing_mode_default = mode; |
| 1108 | break; |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
| 1115 | static void nand_release_data_interface(struct nand_chip *chip) |
| 1116 | { |
| 1117 | kfree(chip->data_interface); |
| 1118 | } |
| 1119 | |
| 1120 | /** |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1121 | * nand_reset - Reset and initialize a NAND device |
| 1122 | * @chip: The NAND chip |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1123 | * @chipnr: Internal die id |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1124 | * |
| 1125 | * Returns 0 for success or negative error code otherwise |
| 1126 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1127 | int nand_reset(struct nand_chip *chip, int chipnr) |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1128 | { |
| 1129 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1130 | int ret; |
| 1131 | |
| 1132 | ret = nand_reset_data_interface(chip); |
| 1133 | if (ret) |
| 1134 | return ret; |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1135 | |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1136 | /* |
| 1137 | * The CS line has to be released before we can apply the new NAND |
| 1138 | * interface settings, hence this weird ->select_chip() dance. |
| 1139 | */ |
| 1140 | chip->select_chip(mtd, chipnr); |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1141 | chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1142 | chip->select_chip(mtd, -1); |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1143 | |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1144 | chip->select_chip(mtd, chipnr); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1145 | ret = nand_setup_data_interface(chip); |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1146 | chip->select_chip(mtd, -1); |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 1147 | if (ret) |
| 1148 | return ret; |
| 1149 | |
Sascha Hauer | 2f94abf | 2016-09-15 10:32:45 +0200 | [diff] [blame] | 1150 | return 0; |
| 1151 | } |
| 1152 | |
| 1153 | /** |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 1154 | * __nand_unlock - [REPLACEABLE] unlocks specified locked blocks |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 1155 | * @mtd: mtd info |
| 1156 | * @ofs: offset to start unlock from |
| 1157 | * @len: length to unlock |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1158 | * @invert: when = 0, unlock the range of blocks within the lower and |
| 1159 | * upper boundary address |
| 1160 | * when = 1, unlock the range of blocks outside the boundaries |
| 1161 | * of the lower and upper boundary address |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1162 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1163 | * Returs unlock status. |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1164 | */ |
| 1165 | static int __nand_unlock(struct mtd_info *mtd, loff_t ofs, |
| 1166 | uint64_t len, int invert) |
| 1167 | { |
| 1168 | int ret = 0; |
| 1169 | int status, page; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 1170 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1171 | |
| 1172 | /* Submit address of first page to unlock */ |
| 1173 | page = ofs >> chip->page_shift; |
| 1174 | chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask); |
| 1175 | |
| 1176 | /* Submit address of last page to unlock */ |
| 1177 | page = (ofs + len) >> chip->page_shift; |
| 1178 | chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1, |
| 1179 | (page | invert) & chip->pagemask); |
| 1180 | |
| 1181 | /* Call wait ready function */ |
| 1182 | status = chip->waitfunc(mtd, chip); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1183 | /* See if device thinks it succeeded */ |
Huang Shijie | 7483096 | 2012-10-14 23:47:24 -0400 | [diff] [blame] | 1184 | if (status & NAND_STATUS_FAIL) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 1185 | pr_debug("%s: error status = 0x%08x\n", |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1186 | __func__, status); |
| 1187 | ret = -EIO; |
| 1188 | } |
| 1189 | |
| 1190 | return ret; |
| 1191 | } |
| 1192 | |
| 1193 | /** |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 1194 | * nand_unlock - [REPLACEABLE] unlocks specified locked blocks |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 1195 | * @mtd: mtd info |
| 1196 | * @ofs: offset to start unlock from |
| 1197 | * @len: length to unlock |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1198 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1199 | * Returns unlock status. |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1200 | */ |
| 1201 | int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 1202 | { |
| 1203 | int ret = 0; |
| 1204 | int chipnr; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 1205 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1206 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 1207 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1208 | __func__, (unsigned long long)ofs, len); |
| 1209 | |
| 1210 | if (check_offs_len(mtd, ofs, len)) |
Brian Norris | b1a2348 | 2015-02-28 02:02:27 -0800 | [diff] [blame] | 1211 | return -EINVAL; |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1212 | |
| 1213 | /* Align to last block address if size addresses end of the device */ |
| 1214 | if (ofs + len == mtd->size) |
| 1215 | len -= mtd->erasesize; |
| 1216 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 1217 | nand_get_device(mtd, FL_UNLOCKING); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1218 | |
| 1219 | /* Shift to get chip number */ |
| 1220 | chipnr = ofs >> chip->chip_shift; |
| 1221 | |
White Ding | 57d3a9a | 2014-07-24 00:10:45 +0800 | [diff] [blame] | 1222 | /* |
| 1223 | * Reset the chip. |
| 1224 | * If we want to check the WP through READ STATUS and check the bit 7 |
| 1225 | * we must reset the chip |
| 1226 | * some operation can also clear the bit 7 of status register |
| 1227 | * eg. erase/program a locked block |
| 1228 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1229 | nand_reset(chip, chipnr); |
| 1230 | |
| 1231 | chip->select_chip(mtd, chipnr); |
White Ding | 57d3a9a | 2014-07-24 00:10:45 +0800 | [diff] [blame] | 1232 | |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1233 | /* Check, if it is write protected */ |
| 1234 | if (nand_check_wp(mtd)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 1235 | pr_debug("%s: device is write protected!\n", |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1236 | __func__); |
| 1237 | ret = -EIO; |
| 1238 | goto out; |
| 1239 | } |
| 1240 | |
| 1241 | ret = __nand_unlock(mtd, ofs, len, 0); |
| 1242 | |
| 1243 | out: |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 1244 | chip->select_chip(mtd, -1); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1245 | nand_release_device(mtd); |
| 1246 | |
| 1247 | return ret; |
| 1248 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1249 | EXPORT_SYMBOL(nand_unlock); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1250 | |
| 1251 | /** |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 1252 | * nand_lock - [REPLACEABLE] locks all blocks present in the device |
Randy Dunlap | b6d676d | 2010-08-10 18:02:50 -0700 | [diff] [blame] | 1253 | * @mtd: mtd info |
| 1254 | * @ofs: offset to start unlock from |
| 1255 | * @len: length to unlock |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1256 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1257 | * This feature is not supported in many NAND parts. 'Micron' NAND parts do |
| 1258 | * have this feature, but it allows only to lock all blocks, not for specified |
| 1259 | * range for block. Implementing 'lock' feature by making use of 'unlock', for |
| 1260 | * now. |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1261 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1262 | * Returns lock status. |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1263 | */ |
| 1264 | int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 1265 | { |
| 1266 | int ret = 0; |
| 1267 | int chipnr, status, page; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 1268 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1269 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 1270 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1271 | __func__, (unsigned long long)ofs, len); |
| 1272 | |
| 1273 | if (check_offs_len(mtd, ofs, len)) |
Brian Norris | b1a2348 | 2015-02-28 02:02:27 -0800 | [diff] [blame] | 1274 | return -EINVAL; |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1275 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 1276 | nand_get_device(mtd, FL_LOCKING); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1277 | |
| 1278 | /* Shift to get chip number */ |
| 1279 | chipnr = ofs >> chip->chip_shift; |
| 1280 | |
White Ding | 57d3a9a | 2014-07-24 00:10:45 +0800 | [diff] [blame] | 1281 | /* |
| 1282 | * Reset the chip. |
| 1283 | * If we want to check the WP through READ STATUS and check the bit 7 |
| 1284 | * we must reset the chip |
| 1285 | * some operation can also clear the bit 7 of status register |
| 1286 | * eg. erase/program a locked block |
| 1287 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 1288 | nand_reset(chip, chipnr); |
| 1289 | |
| 1290 | chip->select_chip(mtd, chipnr); |
White Ding | 57d3a9a | 2014-07-24 00:10:45 +0800 | [diff] [blame] | 1291 | |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1292 | /* Check, if it is write protected */ |
| 1293 | if (nand_check_wp(mtd)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 1294 | pr_debug("%s: device is write protected!\n", |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1295 | __func__); |
| 1296 | status = MTD_ERASE_FAILED; |
| 1297 | ret = -EIO; |
| 1298 | goto out; |
| 1299 | } |
| 1300 | |
| 1301 | /* Submit address of first page to lock */ |
| 1302 | page = ofs >> chip->page_shift; |
| 1303 | chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask); |
| 1304 | |
| 1305 | /* Call wait ready function */ |
| 1306 | status = chip->waitfunc(mtd, chip); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1307 | /* See if device thinks it succeeded */ |
Huang Shijie | 7483096 | 2012-10-14 23:47:24 -0400 | [diff] [blame] | 1308 | if (status & NAND_STATUS_FAIL) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 1309 | pr_debug("%s: error status = 0x%08x\n", |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1310 | __func__, status); |
| 1311 | ret = -EIO; |
| 1312 | goto out; |
| 1313 | } |
| 1314 | |
| 1315 | ret = __nand_unlock(mtd, ofs, len, 0x1); |
| 1316 | |
| 1317 | out: |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 1318 | chip->select_chip(mtd, -1); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1319 | nand_release_device(mtd); |
| 1320 | |
| 1321 | return ret; |
| 1322 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1323 | EXPORT_SYMBOL(nand_lock); |
Vimal Singh | 7d70f33 | 2010-02-08 15:50:49 +0530 | [diff] [blame] | 1324 | |
| 1325 | /** |
Boris BREZILLON | 730a43f | 2015-09-03 18:03:38 +0200 | [diff] [blame] | 1326 | * nand_check_erased_buf - check if a buffer contains (almost) only 0xff data |
| 1327 | * @buf: buffer to test |
| 1328 | * @len: buffer length |
| 1329 | * @bitflips_threshold: maximum number of bitflips |
| 1330 | * |
| 1331 | * Check if a buffer contains only 0xff, which means the underlying region |
| 1332 | * has been erased and is ready to be programmed. |
| 1333 | * The bitflips_threshold specify the maximum number of bitflips before |
| 1334 | * considering the region is not erased. |
| 1335 | * Note: The logic of this function has been extracted from the memweight |
| 1336 | * implementation, except that nand_check_erased_buf function exit before |
| 1337 | * testing the whole buffer if the number of bitflips exceed the |
| 1338 | * bitflips_threshold value. |
| 1339 | * |
| 1340 | * Returns a positive number of bitflips less than or equal to |
| 1341 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 1342 | * threshold. |
| 1343 | */ |
| 1344 | static int nand_check_erased_buf(void *buf, int len, int bitflips_threshold) |
| 1345 | { |
| 1346 | const unsigned char *bitmap = buf; |
| 1347 | int bitflips = 0; |
| 1348 | int weight; |
| 1349 | |
| 1350 | for (; len && ((uintptr_t)bitmap) % sizeof(long); |
| 1351 | len--, bitmap++) { |
| 1352 | weight = hweight8(*bitmap); |
| 1353 | bitflips += BITS_PER_BYTE - weight; |
| 1354 | if (unlikely(bitflips > bitflips_threshold)) |
| 1355 | return -EBADMSG; |
| 1356 | } |
| 1357 | |
| 1358 | for (; len >= sizeof(long); |
| 1359 | len -= sizeof(long), bitmap += sizeof(long)) { |
| 1360 | weight = hweight_long(*((unsigned long *)bitmap)); |
| 1361 | bitflips += BITS_PER_LONG - weight; |
| 1362 | if (unlikely(bitflips > bitflips_threshold)) |
| 1363 | return -EBADMSG; |
| 1364 | } |
| 1365 | |
| 1366 | for (; len > 0; len--, bitmap++) { |
| 1367 | weight = hweight8(*bitmap); |
| 1368 | bitflips += BITS_PER_BYTE - weight; |
| 1369 | if (unlikely(bitflips > bitflips_threshold)) |
| 1370 | return -EBADMSG; |
| 1371 | } |
| 1372 | |
| 1373 | return bitflips; |
| 1374 | } |
| 1375 | |
| 1376 | /** |
| 1377 | * nand_check_erased_ecc_chunk - check if an ECC chunk contains (almost) only |
| 1378 | * 0xff data |
| 1379 | * @data: data buffer to test |
| 1380 | * @datalen: data length |
| 1381 | * @ecc: ECC buffer |
| 1382 | * @ecclen: ECC length |
| 1383 | * @extraoob: extra OOB buffer |
| 1384 | * @extraooblen: extra OOB length |
| 1385 | * @bitflips_threshold: maximum number of bitflips |
| 1386 | * |
| 1387 | * Check if a data buffer and its associated ECC and OOB data contains only |
| 1388 | * 0xff pattern, which means the underlying region has been erased and is |
| 1389 | * ready to be programmed. |
| 1390 | * The bitflips_threshold specify the maximum number of bitflips before |
| 1391 | * considering the region as not erased. |
| 1392 | * |
| 1393 | * Note: |
| 1394 | * 1/ ECC algorithms are working on pre-defined block sizes which are usually |
| 1395 | * different from the NAND page size. When fixing bitflips, ECC engines will |
| 1396 | * report the number of errors per chunk, and the NAND core infrastructure |
| 1397 | * expect you to return the maximum number of bitflips for the whole page. |
| 1398 | * This is why you should always use this function on a single chunk and |
| 1399 | * not on the whole page. After checking each chunk you should update your |
| 1400 | * max_bitflips value accordingly. |
| 1401 | * 2/ When checking for bitflips in erased pages you should not only check |
| 1402 | * the payload data but also their associated ECC data, because a user might |
| 1403 | * have programmed almost all bits to 1 but a few. In this case, we |
| 1404 | * shouldn't consider the chunk as erased, and checking ECC bytes prevent |
| 1405 | * this case. |
| 1406 | * 3/ The extraoob argument is optional, and should be used if some of your OOB |
| 1407 | * data are protected by the ECC engine. |
| 1408 | * It could also be used if you support subpages and want to attach some |
| 1409 | * extra OOB data to an ECC chunk. |
| 1410 | * |
| 1411 | * Returns a positive number of bitflips less than or equal to |
| 1412 | * bitflips_threshold, or -ERROR_CODE for bitflips in excess of the |
| 1413 | * threshold. In case of success, the passed buffers are filled with 0xff. |
| 1414 | */ |
| 1415 | int nand_check_erased_ecc_chunk(void *data, int datalen, |
| 1416 | void *ecc, int ecclen, |
| 1417 | void *extraoob, int extraooblen, |
| 1418 | int bitflips_threshold) |
| 1419 | { |
| 1420 | int data_bitflips = 0, ecc_bitflips = 0, extraoob_bitflips = 0; |
| 1421 | |
| 1422 | data_bitflips = nand_check_erased_buf(data, datalen, |
| 1423 | bitflips_threshold); |
| 1424 | if (data_bitflips < 0) |
| 1425 | return data_bitflips; |
| 1426 | |
| 1427 | bitflips_threshold -= data_bitflips; |
| 1428 | |
| 1429 | ecc_bitflips = nand_check_erased_buf(ecc, ecclen, bitflips_threshold); |
| 1430 | if (ecc_bitflips < 0) |
| 1431 | return ecc_bitflips; |
| 1432 | |
| 1433 | bitflips_threshold -= ecc_bitflips; |
| 1434 | |
| 1435 | extraoob_bitflips = nand_check_erased_buf(extraoob, extraooblen, |
| 1436 | bitflips_threshold); |
| 1437 | if (extraoob_bitflips < 0) |
| 1438 | return extraoob_bitflips; |
| 1439 | |
| 1440 | if (data_bitflips) |
| 1441 | memset(data, 0xff, datalen); |
| 1442 | |
| 1443 | if (ecc_bitflips) |
| 1444 | memset(ecc, 0xff, ecclen); |
| 1445 | |
| 1446 | if (extraoob_bitflips) |
| 1447 | memset(extraoob, 0xff, extraooblen); |
| 1448 | |
| 1449 | return data_bitflips + ecc_bitflips + extraoob_bitflips; |
| 1450 | } |
| 1451 | EXPORT_SYMBOL(nand_check_erased_ecc_chunk); |
| 1452 | |
| 1453 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1454 | * nand_read_page_raw - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1455 | * @mtd: mtd info structure |
| 1456 | * @chip: nand chip info structure |
| 1457 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1458 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1459 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1460 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1461 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1462 | */ |
| 1463 | static int nand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1464 | uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1465 | { |
| 1466 | chip->read_buf(mtd, buf, mtd->writesize); |
Brian Norris | 279f08d | 2012-05-02 10:15:03 -0700 | [diff] [blame] | 1467 | if (oob_required) |
| 1468 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1469 | return 0; |
| 1470 | } |
| 1471 | |
| 1472 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1473 | * nand_read_page_raw_syndrome - [INTERN] read raw page data without ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1474 | * @mtd: mtd info structure |
| 1475 | * @chip: nand chip info structure |
| 1476 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1477 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1478 | * @page: page number to read |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1479 | * |
| 1480 | * We need a special oob layout and handling even when OOB isn't used. |
| 1481 | */ |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1482 | static int nand_read_page_raw_syndrome(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1483 | struct nand_chip *chip, uint8_t *buf, |
| 1484 | int oob_required, int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 1485 | { |
| 1486 | int eccsize = chip->ecc.size; |
| 1487 | int eccbytes = chip->ecc.bytes; |
| 1488 | uint8_t *oob = chip->oob_poi; |
| 1489 | int steps, size; |
| 1490 | |
| 1491 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
| 1492 | chip->read_buf(mtd, buf, eccsize); |
| 1493 | buf += eccsize; |
| 1494 | |
| 1495 | if (chip->ecc.prepad) { |
| 1496 | chip->read_buf(mtd, oob, chip->ecc.prepad); |
| 1497 | oob += chip->ecc.prepad; |
| 1498 | } |
| 1499 | |
| 1500 | chip->read_buf(mtd, oob, eccbytes); |
| 1501 | oob += eccbytes; |
| 1502 | |
| 1503 | if (chip->ecc.postpad) { |
| 1504 | chip->read_buf(mtd, oob, chip->ecc.postpad); |
| 1505 | oob += chip->ecc.postpad; |
| 1506 | } |
| 1507 | } |
| 1508 | |
| 1509 | size = mtd->oobsize - (oob - chip->oob_poi); |
| 1510 | if (size) |
| 1511 | chip->read_buf(mtd, oob, size); |
| 1512 | |
| 1513 | return 0; |
| 1514 | } |
| 1515 | |
| 1516 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1517 | * nand_read_page_swecc - [REPLACEABLE] software ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1518 | * @mtd: mtd info structure |
| 1519 | * @chip: nand chip info structure |
| 1520 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1521 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1522 | * @page: page number to read |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1523 | */ |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1524 | static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1525 | uint8_t *buf, int oob_required, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1527 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1528 | int eccbytes = chip->ecc.bytes; |
| 1529 | int eccsteps = chip->ecc.steps; |
| 1530 | uint8_t *p = buf; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 1531 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1532 | uint8_t *ecc_code = chip->buffers->ecccode; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1533 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1534 | |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1535 | chip->ecc.read_page_raw(mtd, chip, buf, 1, page); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1536 | |
| 1537 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 1538 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1539 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1540 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 1541 | chip->ecc.total); |
| 1542 | if (ret) |
| 1543 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1544 | |
| 1545 | eccsteps = chip->ecc.steps; |
| 1546 | p = buf; |
| 1547 | |
| 1548 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1549 | int stat; |
| 1550 | |
| 1551 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1552 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1553 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1554 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1555 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1556 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1557 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1558 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1559 | return max_bitflips; |
Thomas Gleixner | 22c60f5 | 2005-04-04 19:56:32 +0100 | [diff] [blame] | 1560 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | /** |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 1563 | * nand_read_subpage - [REPLACEABLE] ECC based sub-page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1564 | * @mtd: mtd info structure |
| 1565 | * @chip: nand chip info structure |
| 1566 | * @data_offs: offset of requested data within the page |
| 1567 | * @readlen: data length |
| 1568 | * @bufpoi: buffer to store read data |
Huang Shijie | e004deb | 2014-01-03 11:01:40 +0800 | [diff] [blame] | 1569 | * @page: page number to read |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1570 | */ |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1571 | static int nand_read_subpage(struct mtd_info *mtd, struct nand_chip *chip, |
Huang Shijie | e004deb | 2014-01-03 11:01:40 +0800 | [diff] [blame] | 1572 | uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi, |
| 1573 | int page) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1574 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1575 | int start_step, end_step, num_steps, ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1576 | uint8_t *p; |
| 1577 | int data_col_addr, i, gaps = 0; |
| 1578 | int datafrag_len, eccfrag_len, aligned_len, aligned_pos; |
| 1579 | int busw = (chip->options & NAND_BUSWIDTH_16) ? 2 : 1; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1580 | int index, section = 0; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1581 | unsigned int max_bitflips = 0; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1582 | struct mtd_oob_region oobregion = { }; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1583 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1584 | /* Column address within the page aligned to ECC size (256bytes) */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1585 | start_step = data_offs / chip->ecc.size; |
| 1586 | end_step = (data_offs + readlen - 1) / chip->ecc.size; |
| 1587 | num_steps = end_step - start_step + 1; |
Ron | 4a4163ca | 2014-03-16 04:01:07 +1030 | [diff] [blame] | 1588 | index = start_step * chip->ecc.bytes; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1589 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1590 | /* Data size aligned to ECC ecc.size */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1591 | datafrag_len = num_steps * chip->ecc.size; |
| 1592 | eccfrag_len = num_steps * chip->ecc.bytes; |
| 1593 | |
| 1594 | data_col_addr = start_step * chip->ecc.size; |
| 1595 | /* If we read not a page aligned data */ |
| 1596 | if (data_col_addr != 0) |
| 1597 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_col_addr, -1); |
| 1598 | |
| 1599 | p = bufpoi + data_col_addr; |
| 1600 | chip->read_buf(mtd, p, datafrag_len); |
| 1601 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1602 | /* Calculate ECC */ |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1603 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) |
| 1604 | chip->ecc.calculate(mtd, p, &chip->buffers->ecccalc[i]); |
| 1605 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1606 | /* |
| 1607 | * The performance is faster if we position offsets according to |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1608 | * 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] | 1609 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1610 | ret = mtd_ooblayout_find_eccregion(mtd, index, §ion, &oobregion); |
| 1611 | if (ret) |
| 1612 | return ret; |
| 1613 | |
| 1614 | if (oobregion.length < eccfrag_len) |
| 1615 | gaps = 1; |
| 1616 | |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1617 | if (gaps) { |
| 1618 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1); |
| 1619 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1620 | } else { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1621 | /* |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1622 | * Send the command to read the particular ECC bytes take care |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1623 | * about buswidth alignment in read_buf. |
| 1624 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1625 | aligned_pos = oobregion.offset & ~(busw - 1); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1626 | aligned_len = eccfrag_len; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1627 | if (oobregion.offset & (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1628 | aligned_len++; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1629 | if ((oobregion.offset + (num_steps * chip->ecc.bytes)) & |
| 1630 | (busw - 1)) |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1631 | aligned_len++; |
| 1632 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1633 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1634 | mtd->writesize + aligned_pos, -1); |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1635 | chip->read_buf(mtd, &chip->oob_poi[aligned_pos], aligned_len); |
| 1636 | } |
| 1637 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1638 | ret = mtd_ooblayout_get_eccbytes(mtd, chip->buffers->ecccode, |
| 1639 | chip->oob_poi, index, eccfrag_len); |
| 1640 | if (ret) |
| 1641 | return ret; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1642 | |
| 1643 | p = bufpoi + data_col_addr; |
| 1644 | for (i = 0; i < eccfrag_len ; i += chip->ecc.bytes, p += chip->ecc.size) { |
| 1645 | int stat; |
| 1646 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1647 | stat = chip->ecc.correct(mtd, p, |
| 1648 | &chip->buffers->ecccode[i], &chip->buffers->ecccalc[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 1649 | if (stat == -EBADMSG && |
| 1650 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1651 | /* check for empty pages with bitflips */ |
| 1652 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 1653 | &chip->buffers->ecccode[i], |
| 1654 | chip->ecc.bytes, |
| 1655 | NULL, 0, |
| 1656 | chip->ecc.strength); |
| 1657 | } |
| 1658 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1659 | if (stat < 0) { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1660 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1661 | } else { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1662 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1663 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1664 | } |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1665 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1666 | return max_bitflips; |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1670 | * nand_read_page_hwecc - [REPLACEABLE] hardware ECC based page read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1671 | * @mtd: mtd info structure |
| 1672 | * @chip: nand chip info structure |
| 1673 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1674 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1675 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1676 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1677 | * Not for syndrome calculating ECC controllers which need a special oob layout. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1678 | */ |
| 1679 | static int nand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1680 | uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1681 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1682 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1683 | int eccbytes = chip->ecc.bytes; |
| 1684 | int eccsteps = chip->ecc.steps; |
| 1685 | uint8_t *p = buf; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 1686 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 1687 | uint8_t *ecc_code = chip->buffers->ecccode; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1688 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1689 | |
| 1690 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1691 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 1692 | chip->read_buf(mtd, p, eccsize); |
| 1693 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1694 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1695 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1696 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1697 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 1698 | chip->ecc.total); |
| 1699 | if (ret) |
| 1700 | return ret; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1701 | |
| 1702 | eccsteps = chip->ecc.steps; |
| 1703 | p = buf; |
| 1704 | |
| 1705 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1706 | int stat; |
| 1707 | |
| 1708 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 1709 | if (stat == -EBADMSG && |
| 1710 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1711 | /* check for empty pages with bitflips */ |
| 1712 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 1713 | &ecc_code[i], eccbytes, |
| 1714 | NULL, 0, |
| 1715 | chip->ecc.strength); |
| 1716 | } |
| 1717 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1718 | if (stat < 0) { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1719 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1720 | } else { |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1721 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1722 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1723 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1724 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1725 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1729 | * nand_read_page_hwecc_oob_first - [REPLACEABLE] hw ecc, read oob first |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1730 | * @mtd: mtd info structure |
| 1731 | * @chip: nand chip info structure |
| 1732 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1733 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1734 | * @page: page number to read |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1735 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1736 | * Hardware ECC for large page chips, require OOB to be read first. For this |
| 1737 | * ECC mode, the write_page method is re-used from ECC_HW. These methods |
| 1738 | * read/write ECC from the OOB area, unlike the ECC_HW_SYNDROME support with |
| 1739 | * multiple ECC steps, follows the "infix ECC" scheme and reads/writes ECC from |
| 1740 | * the data area, by overwriting the NAND manufacturer bad block markings. |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1741 | */ |
| 1742 | static int nand_read_page_hwecc_oob_first(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1743 | struct nand_chip *chip, uint8_t *buf, int oob_required, int page) |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1744 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1745 | int i, eccsize = chip->ecc.size, ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1746 | int eccbytes = chip->ecc.bytes; |
| 1747 | int eccsteps = chip->ecc.steps; |
| 1748 | uint8_t *p = buf; |
| 1749 | uint8_t *ecc_code = chip->buffers->ecccode; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1750 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1751 | unsigned int max_bitflips = 0; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1752 | |
| 1753 | /* Read the OOB area first */ |
| 1754 | chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); |
| 1755 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1756 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page); |
| 1757 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1758 | ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, |
| 1759 | chip->ecc.total); |
| 1760 | if (ret) |
| 1761 | return ret; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1762 | |
| 1763 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1764 | int stat; |
| 1765 | |
| 1766 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 1767 | chip->read_buf(mtd, p, eccsize); |
| 1768 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 1769 | |
| 1770 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL); |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 1771 | if (stat == -EBADMSG && |
| 1772 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1773 | /* check for empty pages with bitflips */ |
| 1774 | stat = nand_check_erased_ecc_chunk(p, eccsize, |
| 1775 | &ecc_code[i], eccbytes, |
| 1776 | NULL, 0, |
| 1777 | chip->ecc.strength); |
| 1778 | } |
| 1779 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1780 | if (stat < 0) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1781 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1782 | } else { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1783 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1784 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1785 | } |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1786 | } |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1787 | return max_bitflips; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1791 | * nand_read_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page read |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1792 | * @mtd: mtd info structure |
| 1793 | * @chip: nand chip info structure |
| 1794 | * @buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1795 | * @oob_required: caller requires OOB data read to chip->oob_poi |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1796 | * @page: page number to read |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1797 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1798 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 1799 | * need a special oob layout and handling. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1800 | */ |
| 1801 | static int nand_read_page_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1802 | uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1803 | { |
| 1804 | int i, eccsize = chip->ecc.size; |
| 1805 | int eccbytes = chip->ecc.bytes; |
| 1806 | int eccsteps = chip->ecc.steps; |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 1807 | int eccpadbytes = eccbytes + chip->ecc.prepad + chip->ecc.postpad; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1808 | uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 1809 | uint8_t *oob = chip->oob_poi; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1810 | unsigned int max_bitflips = 0; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1811 | |
| 1812 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 1813 | int stat; |
| 1814 | |
| 1815 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 1816 | chip->read_buf(mtd, p, eccsize); |
| 1817 | |
| 1818 | if (chip->ecc.prepad) { |
| 1819 | chip->read_buf(mtd, oob, chip->ecc.prepad); |
| 1820 | oob += chip->ecc.prepad; |
| 1821 | } |
| 1822 | |
| 1823 | chip->ecc.hwctl(mtd, NAND_ECC_READSYN); |
| 1824 | chip->read_buf(mtd, oob, eccbytes); |
| 1825 | stat = chip->ecc.correct(mtd, p, oob, NULL); |
| 1826 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1827 | oob += eccbytes; |
| 1828 | |
| 1829 | if (chip->ecc.postpad) { |
| 1830 | chip->read_buf(mtd, oob, chip->ecc.postpad); |
| 1831 | oob += chip->ecc.postpad; |
| 1832 | } |
Boris BREZILLON | 40cbe6e | 2015-12-30 20:32:04 +0100 | [diff] [blame] | 1833 | |
| 1834 | if (stat == -EBADMSG && |
| 1835 | (chip->ecc.options & NAND_ECC_GENERIC_ERASED_CHECK)) { |
| 1836 | /* check for empty pages with bitflips */ |
| 1837 | stat = nand_check_erased_ecc_chunk(p, chip->ecc.size, |
| 1838 | oob - eccpadbytes, |
| 1839 | eccpadbytes, |
| 1840 | NULL, 0, |
| 1841 | chip->ecc.strength); |
| 1842 | } |
| 1843 | |
| 1844 | if (stat < 0) { |
| 1845 | mtd->ecc_stats.failed++; |
| 1846 | } else { |
| 1847 | mtd->ecc_stats.corrected += stat; |
| 1848 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1849 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 1853 | i = mtd->oobsize - (oob - chip->oob_poi); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1854 | if (i) |
| 1855 | chip->read_buf(mtd, oob, i); |
| 1856 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1857 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1861 | * nand_transfer_oob - [INTERN] Transfer oob to client buffer |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1862 | * @mtd: mtd info structure |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1863 | * @oob: oob destination address |
| 1864 | * @ops: oob ops structure |
| 1865 | * @len: size of oob to transfer |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1866 | */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1867 | 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] | 1868 | struct mtd_oob_ops *ops, size_t len) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1869 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1870 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 1871 | int ret; |
| 1872 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 1873 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1874 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 1875 | case MTD_OPS_PLACE_OOB: |
| 1876 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1877 | memcpy(oob, chip->oob_poi + ops->ooboffs, len); |
| 1878 | return oob + len; |
| 1879 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 1880 | case MTD_OPS_AUTO_OOB: |
| 1881 | ret = mtd_ooblayout_get_databytes(mtd, oob, chip->oob_poi, |
| 1882 | ops->ooboffs, len); |
| 1883 | BUG_ON(ret); |
| 1884 | return oob + len; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1885 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1886 | default: |
| 1887 | BUG(); |
| 1888 | } |
| 1889 | return NULL; |
| 1890 | } |
| 1891 | |
| 1892 | /** |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 1893 | * nand_setup_read_retry - [INTERN] Set the READ RETRY mode |
| 1894 | * @mtd: MTD device structure |
| 1895 | * @retry_mode: the retry mode to use |
| 1896 | * |
| 1897 | * Some vendors supply a special command to shift the Vt threshold, to be used |
| 1898 | * when there are too many bitflips in a page (i.e., ECC error). After setting |
| 1899 | * a new threshold, the host should retry reading the page. |
| 1900 | */ |
| 1901 | static int nand_setup_read_retry(struct mtd_info *mtd, int retry_mode) |
| 1902 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 1903 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 1904 | |
| 1905 | pr_debug("setting READ RETRY mode %d\n", retry_mode); |
| 1906 | |
| 1907 | if (retry_mode >= chip->read_retries) |
| 1908 | return -EINVAL; |
| 1909 | |
| 1910 | if (!chip->setup_read_retry) |
| 1911 | return -EOPNOTSUPP; |
| 1912 | |
| 1913 | return chip->setup_read_retry(mtd, retry_mode); |
| 1914 | } |
| 1915 | |
| 1916 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 1917 | * nand_do_read_ops - [INTERN] Read data with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1918 | * @mtd: MTD device structure |
| 1919 | * @from: offset to read from |
| 1920 | * @ops: oob ops structure |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1921 | * |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1922 | * Internal function. Called with chip held. |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1923 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1924 | static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, |
| 1925 | struct mtd_oob_ops *ops) |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1926 | { |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 1927 | int chipnr, page, realpage, col, bytes, aligned, oob_required; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 1928 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1929 | int ret = 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1930 | uint32_t readlen = ops->len; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 1931 | uint32_t oobreadlen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 1932 | uint32_t max_oobsize = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 9aca334 | 2010-02-22 20:39:35 +0200 | [diff] [blame] | 1933 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1934 | uint8_t *bufpoi, *oob, *buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 1935 | int use_bufpoi; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 1936 | unsigned int max_bitflips = 0; |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 1937 | int retry_mode = 0; |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 1938 | bool ecc_fail = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1940 | chipnr = (int)(from >> chip->chip_shift); |
| 1941 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 1943 | realpage = (int)(from >> chip->page_shift); |
| 1944 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1946 | col = (int)(from & (mtd->writesize - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1948 | buf = ops->datbuf; |
| 1949 | oob = ops->oobbuf; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 1950 | oob_required = oob ? 1 : 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1951 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 1952 | while (1) { |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 1953 | unsigned int ecc_failures = mtd->ecc_stats.failed; |
| 1954 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1955 | bytes = min(mtd->writesize - col, readlen); |
| 1956 | aligned = (bytes == mtd->writesize); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1957 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 1958 | if (!aligned) |
| 1959 | use_bufpoi = 1; |
| 1960 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
| 1961 | use_bufpoi = !virt_addr_valid(buf); |
| 1962 | else |
| 1963 | use_bufpoi = 0; |
| 1964 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 1965 | /* Is the current page in the buffer? */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 1966 | if (realpage != chip->pagebuf || oob) { |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 1967 | bufpoi = use_bufpoi ? chip->buffers->databuf : buf; |
| 1968 | |
| 1969 | if (use_bufpoi && aligned) |
| 1970 | pr_debug("%s: using read bounce buffer for buf@%p\n", |
| 1971 | __func__, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 1973 | read_retry: |
Marc Gonzalez | 3371d66 | 2016-11-15 10:56:20 +0100 | [diff] [blame] | 1974 | if (nand_standard_page_accessors(&chip->ecc)) |
| 1975 | chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 1977 | /* |
| 1978 | * Now read the page into the buffer. Absent an error, |
| 1979 | * the read methods return max bitflips per ecc step. |
| 1980 | */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 1981 | if (unlikely(ops->mode == MTD_OPS_RAW)) |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1982 | ret = chip->ecc.read_page_raw(mtd, chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 1983 | oob_required, |
| 1984 | page); |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 1985 | else if (!aligned && NAND_HAS_SUBPAGE_READ(chip) && |
| 1986 | !oob) |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 1987 | ret = chip->ecc.read_subpage(mtd, chip, |
Huang Shijie | e004deb | 2014-01-03 11:01:40 +0800 | [diff] [blame] | 1988 | col, bytes, bufpoi, |
| 1989 | page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 1990 | else |
Sneha Narnakaje | 46a8cf2 | 2009-09-18 12:51:46 -0700 | [diff] [blame] | 1991 | ret = chip->ecc.read_page(mtd, chip, bufpoi, |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 1992 | oob_required, page); |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 1993 | if (ret < 0) { |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 1994 | if (use_bufpoi) |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 1995 | /* Invalidate page cache */ |
| 1996 | chip->pagebuf = -1; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1997 | break; |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 1998 | } |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 1999 | |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2000 | max_bitflips = max_t(unsigned int, max_bitflips, ret); |
| 2001 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2002 | /* Transfer not aligned data */ |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2003 | if (use_bufpoi) { |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 2004 | if (!NAND_HAS_SUBPAGE_READ(chip) && !oob && |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 2005 | !(mtd->ecc_stats.failed - ecc_failures) && |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2006 | (ops->mode != MTD_OPS_RAW)) { |
Alexey Korolev | 3d45955 | 2008-05-15 17:23:18 +0100 | [diff] [blame] | 2007 | chip->pagebuf = realpage; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2008 | chip->pagebuf_bitflips = ret; |
| 2009 | } else { |
Brian Norris | 6d77b9d | 2011-09-07 13:13:40 -0700 | [diff] [blame] | 2010 | /* Invalidate page cache */ |
| 2011 | chip->pagebuf = -1; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2012 | } |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 2013 | memcpy(buf, chip->buffers->databuf + col, bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2015 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2016 | if (unlikely(oob)) { |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 2017 | int toread = min(oobreadlen, max_oobsize); |
| 2018 | |
| 2019 | if (toread) { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2020 | oob = nand_transfer_oob(mtd, |
Maxim Levitsky | b64d39d | 2010-02-22 20:39:37 +0200 | [diff] [blame] | 2021 | oob, ops, toread); |
| 2022 | oobreadlen -= toread; |
| 2023 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2024 | } |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 2025 | |
| 2026 | if (chip->options & NAND_NEED_READRDY) { |
| 2027 | /* Apply delay or wait for ready/busy pin */ |
| 2028 | if (!chip->dev_ready) |
| 2029 | udelay(chip->chip_delay); |
| 2030 | else |
| 2031 | nand_wait_ready(mtd); |
| 2032 | } |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 2033 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2034 | if (mtd->ecc_stats.failed - ecc_failures) { |
Brian Norris | 28fa65e | 2014-02-12 16:08:28 -0800 | [diff] [blame] | 2035 | if (retry_mode + 1 < chip->read_retries) { |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2036 | retry_mode++; |
| 2037 | ret = nand_setup_read_retry(mtd, |
| 2038 | retry_mode); |
| 2039 | if (ret < 0) |
| 2040 | break; |
| 2041 | |
| 2042 | /* Reset failures; retry */ |
| 2043 | mtd->ecc_stats.failed = ecc_failures; |
| 2044 | goto read_retry; |
| 2045 | } else { |
| 2046 | /* No more retry modes; real failure */ |
| 2047 | ecc_fail = true; |
| 2048 | } |
| 2049 | } |
| 2050 | |
| 2051 | buf += bytes; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2052 | } else { |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 2053 | memcpy(buf, chip->buffers->databuf + col, bytes); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2054 | buf += bytes; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2055 | max_bitflips = max_t(unsigned int, max_bitflips, |
| 2056 | chip->pagebuf_bitflips); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2057 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2059 | readlen -= bytes; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2060 | |
Brian Norris | ba84fb5 | 2014-01-03 15:13:33 -0800 | [diff] [blame] | 2061 | /* Reset to retry mode 0 */ |
| 2062 | if (retry_mode) { |
| 2063 | ret = nand_setup_read_retry(mtd, 0); |
| 2064 | if (ret < 0) |
| 2065 | break; |
| 2066 | retry_mode = 0; |
| 2067 | } |
| 2068 | |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2069 | if (!readlen) |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2070 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2072 | /* For subsequent reads align to page boundary */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | col = 0; |
| 2074 | /* Increment page address */ |
| 2075 | realpage++; |
| 2076 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2077 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | /* Check, if we cross a chip boundary */ |
| 2079 | if (!page) { |
| 2080 | chipnr++; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2081 | chip->select_chip(mtd, -1); |
| 2082 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | } |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2085 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2087 | ops->retlen = ops->len - (size_t) readlen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2088 | if (oob) |
| 2089 | ops->oobretlen = ops->ooblen - oobreadlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 2091 | if (ret < 0) |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2092 | return ret; |
| 2093 | |
Brian Norris | b72f3df | 2013-12-03 11:04:14 -0800 | [diff] [blame] | 2094 | if (ecc_fail) |
Thomas Gleixner | 9a1fcdf | 2006-05-29 14:56:39 +0200 | [diff] [blame] | 2095 | return -EBADMSG; |
| 2096 | |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 2097 | return max_bitflips; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2098 | } |
| 2099 | |
| 2100 | /** |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2101 | * nand_read - [MTD Interface] MTD compatibility function for nand_do_read_ecc |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2102 | * @mtd: MTD device structure |
| 2103 | * @from: offset to read from |
| 2104 | * @len: number of bytes to read |
| 2105 | * @retlen: pointer to variable to store the number of read bytes |
| 2106 | * @buf: the databuffer to put data |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2107 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2108 | * Get hold of the chip and call nand_do_read. |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2109 | */ |
| 2110 | static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 2111 | size_t *retlen, uint8_t *buf) |
| 2112 | { |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2113 | struct mtd_oob_ops ops; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2114 | int ret; |
| 2115 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 2116 | nand_get_device(mtd, FL_READING); |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 2117 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2118 | ops.len = len; |
| 2119 | ops.datbuf = buf; |
Huang Shijie | 11041ae6 | 2012-07-03 16:44:14 +0800 | [diff] [blame] | 2120 | ops.mode = MTD_OPS_PLACE_OOB; |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2121 | ret = nand_do_read_ops(mtd, from, &ops); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2122 | *retlen = ops.retlen; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2123 | nand_release_device(mtd); |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 2124 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | } |
| 2126 | |
| 2127 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2128 | * nand_read_oob_std - [REPLACEABLE] the most common OOB data read function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2129 | * @mtd: mtd info structure |
| 2130 | * @chip: nand chip info structure |
| 2131 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2132 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2133 | int nand_read_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2134 | { |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 2135 | chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2136 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 2137 | return 0; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2138 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2139 | EXPORT_SYMBOL(nand_read_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2140 | |
| 2141 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2142 | * nand_read_oob_syndrome - [REPLACEABLE] OOB data read function for HW ECC |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2143 | * with syndromes |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2144 | * @mtd: mtd info structure |
| 2145 | * @chip: nand chip info structure |
| 2146 | * @page: page number to read |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2147 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2148 | int nand_read_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
| 2149 | int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2150 | { |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2151 | int length = mtd->oobsize; |
| 2152 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 2153 | int eccsize = chip->ecc.size; |
Baruch Siach | 2ea69d2 | 2015-01-22 15:23:05 +0200 | [diff] [blame] | 2154 | uint8_t *bufpoi = chip->oob_poi; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2155 | int i, toread, sndrnd = 0, pos; |
| 2156 | |
| 2157 | chip->cmdfunc(mtd, NAND_CMD_READ0, chip->ecc.size, page); |
| 2158 | for (i = 0; i < chip->ecc.steps; i++) { |
| 2159 | if (sndrnd) { |
| 2160 | pos = eccsize + i * (eccsize + chunk); |
| 2161 | if (mtd->writesize > 512) |
| 2162 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, pos, -1); |
| 2163 | else |
| 2164 | chip->cmdfunc(mtd, NAND_CMD_READ0, pos, page); |
| 2165 | } else |
| 2166 | sndrnd = 1; |
| 2167 | toread = min_t(int, length, chunk); |
| 2168 | chip->read_buf(mtd, bufpoi, toread); |
| 2169 | bufpoi += toread; |
| 2170 | length -= toread; |
| 2171 | } |
| 2172 | if (length > 0) |
| 2173 | chip->read_buf(mtd, bufpoi, length); |
| 2174 | |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 2175 | return 0; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2176 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2177 | EXPORT_SYMBOL(nand_read_oob_syndrome); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2178 | |
| 2179 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2180 | * nand_write_oob_std - [REPLACEABLE] the most common OOB data write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2181 | * @mtd: mtd info structure |
| 2182 | * @chip: nand chip info structure |
| 2183 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2184 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2185 | int nand_write_oob_std(struct mtd_info *mtd, struct nand_chip *chip, int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2186 | { |
| 2187 | int status = 0; |
| 2188 | const uint8_t *buf = chip->oob_poi; |
| 2189 | int length = mtd->oobsize; |
| 2190 | |
| 2191 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page); |
| 2192 | chip->write_buf(mtd, buf, length); |
| 2193 | /* Send command to program the OOB data */ |
| 2194 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 2195 | |
| 2196 | status = chip->waitfunc(mtd, chip); |
| 2197 | |
Savin Zlobec | 0d420f9 | 2006-06-21 11:51:20 +0200 | [diff] [blame] | 2198 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2199 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2200 | EXPORT_SYMBOL(nand_write_oob_std); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2201 | |
| 2202 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2203 | * nand_write_oob_syndrome - [REPLACEABLE] OOB data write function for HW ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2204 | * with syndrome - only for large page flash |
| 2205 | * @mtd: mtd info structure |
| 2206 | * @chip: nand chip info structure |
| 2207 | * @page: page number to write |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2208 | */ |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2209 | int nand_write_oob_syndrome(struct mtd_info *mtd, struct nand_chip *chip, |
| 2210 | int page) |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2211 | { |
| 2212 | int chunk = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad; |
| 2213 | int eccsize = chip->ecc.size, length = mtd->oobsize; |
| 2214 | int i, len, pos, status = 0, sndcmd = 0, steps = chip->ecc.steps; |
| 2215 | const uint8_t *bufpoi = chip->oob_poi; |
| 2216 | |
| 2217 | /* |
| 2218 | * data-ecc-data-ecc ... ecc-oob |
| 2219 | * or |
| 2220 | * data-pad-ecc-pad-data-pad .... ecc-pad-oob |
| 2221 | */ |
| 2222 | if (!chip->ecc.prepad && !chip->ecc.postpad) { |
| 2223 | pos = steps * (eccsize + chunk); |
| 2224 | steps = 0; |
| 2225 | } else |
Vitaly Wool | 8b0036e | 2006-07-11 09:11:25 +0200 | [diff] [blame] | 2226 | pos = eccsize; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2227 | |
| 2228 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, pos, page); |
| 2229 | for (i = 0; i < steps; i++) { |
| 2230 | if (sndcmd) { |
| 2231 | if (mtd->writesize <= 512) { |
| 2232 | uint32_t fill = 0xFFFFFFFF; |
| 2233 | |
| 2234 | len = eccsize; |
| 2235 | while (len > 0) { |
| 2236 | int num = min_t(int, len, 4); |
| 2237 | chip->write_buf(mtd, (uint8_t *)&fill, |
| 2238 | num); |
| 2239 | len -= num; |
| 2240 | } |
| 2241 | } else { |
| 2242 | pos = eccsize + i * (eccsize + chunk); |
| 2243 | chip->cmdfunc(mtd, NAND_CMD_RNDIN, pos, -1); |
| 2244 | } |
| 2245 | } else |
| 2246 | sndcmd = 1; |
| 2247 | len = min_t(int, length, chunk); |
| 2248 | chip->write_buf(mtd, bufpoi, len); |
| 2249 | bufpoi += len; |
| 2250 | length -= len; |
| 2251 | } |
| 2252 | if (length > 0) |
| 2253 | chip->write_buf(mtd, bufpoi, length); |
| 2254 | |
| 2255 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 2256 | status = chip->waitfunc(mtd, chip); |
| 2257 | |
| 2258 | return status & NAND_STATUS_FAIL ? -EIO : 0; |
| 2259 | } |
Boris Brezillon | 9d02fc2 | 2015-08-26 16:08:12 +0200 | [diff] [blame] | 2260 | EXPORT_SYMBOL(nand_write_oob_syndrome); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2261 | |
| 2262 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2263 | * nand_do_read_oob - [INTERN] NAND read out-of-band |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2264 | * @mtd: MTD device structure |
| 2265 | * @from: offset to read from |
| 2266 | * @ops: oob operations description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2268 | * NAND read out-of-band data from the spare area. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2270 | static int nand_do_read_oob(struct mtd_info *mtd, loff_t from, |
| 2271 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | { |
Brian Norris | c00a099 | 2012-05-01 17:12:54 -0700 | [diff] [blame] | 2273 | int page, realpage, chipnr; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2274 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 2275 | struct mtd_ecc_stats stats; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2276 | int readlen = ops->ooblen; |
| 2277 | int len; |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2278 | uint8_t *buf = ops->oobbuf; |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 2279 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2281 | pr_debug("%s: from = 0x%08Lx, len = %i\n", |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 2282 | __func__, (unsigned long long)from, readlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 2284 | stats = mtd->ecc_stats; |
| 2285 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 2286 | len = mtd_oobavail(mtd, ops); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2287 | |
| 2288 | if (unlikely(ops->ooboffs >= len)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2289 | pr_debug("%s: attempt to start read outside oob\n", |
| 2290 | __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2291 | return -EINVAL; |
| 2292 | } |
| 2293 | |
| 2294 | /* Do not allow reads past end of device */ |
| 2295 | if (unlikely(from >= mtd->size || |
| 2296 | ops->ooboffs + readlen > ((mtd->size >> chip->page_shift) - |
| 2297 | (from >> chip->page_shift)) * len)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2298 | pr_debug("%s: attempt to read beyond end of device\n", |
| 2299 | __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2300 | return -EINVAL; |
| 2301 | } |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2302 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2303 | chipnr = (int)(from >> chip->chip_shift); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 2304 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2306 | /* Shift to get page */ |
| 2307 | realpage = (int)(from >> chip->page_shift); |
| 2308 | page = realpage & chip->pagemask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2310 | while (1) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2311 | if (ops->mode == MTD_OPS_RAW) |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 2312 | ret = chip->ecc.read_oob_raw(mtd, chip, page); |
Brian Norris | c46f648 | 2011-08-30 18:45:38 -0700 | [diff] [blame] | 2313 | else |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 2314 | ret = chip->ecc.read_oob(mtd, chip, page); |
| 2315 | |
| 2316 | if (ret < 0) |
| 2317 | break; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2318 | |
| 2319 | len = min(len, readlen); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2320 | buf = nand_transfer_oob(mtd, buf, ops, len); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2321 | |
Brian Norris | 5bc7c33 | 2013-03-13 09:51:31 -0700 | [diff] [blame] | 2322 | if (chip->options & NAND_NEED_READRDY) { |
| 2323 | /* Apply delay or wait for ready/busy pin */ |
| 2324 | if (!chip->dev_ready) |
| 2325 | udelay(chip->chip_delay); |
| 2326 | else |
| 2327 | nand_wait_ready(mtd); |
| 2328 | } |
| 2329 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2330 | readlen -= len; |
Savin Zlobec | 0d420f9 | 2006-06-21 11:51:20 +0200 | [diff] [blame] | 2331 | if (!readlen) |
| 2332 | break; |
| 2333 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2334 | /* Increment page address */ |
| 2335 | realpage++; |
| 2336 | |
| 2337 | page = realpage & chip->pagemask; |
| 2338 | /* Check, if we cross a chip boundary */ |
| 2339 | if (!page) { |
| 2340 | chipnr++; |
| 2341 | chip->select_chip(mtd, -1); |
| 2342 | chip->select_chip(mtd, chipnr); |
| 2343 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | } |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2345 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | |
Shmulik Ladkani | 1951f2f | 2012-05-09 13:13:34 +0300 | [diff] [blame] | 2347 | ops->oobretlen = ops->ooblen - readlen; |
| 2348 | |
| 2349 | if (ret < 0) |
| 2350 | return ret; |
Brian Norris | 041e457 | 2011-06-23 16:45:24 -0700 | [diff] [blame] | 2351 | |
| 2352 | if (mtd->ecc_stats.failed - stats.failed) |
| 2353 | return -EBADMSG; |
| 2354 | |
| 2355 | return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2359 | * 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] | 2360 | * @mtd: MTD device structure |
| 2361 | * @from: offset to read from |
| 2362 | * @ops: oob operation description structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2364 | * NAND read data and/or out-of-band data. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2366 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, |
| 2367 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | { |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 2369 | int ret; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2370 | |
| 2371 | ops->retlen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | |
| 2373 | /* Do not allow reads past end of device */ |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2374 | if (ops->datbuf && (from + ops->len) > mtd->size) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2375 | pr_debug("%s: attempt to read beyond end of device\n", |
| 2376 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | return -EINVAL; |
| 2378 | } |
| 2379 | |
Andrey Smirnov | fc6b4d1 | 2016-07-21 14:59:21 -0700 | [diff] [blame] | 2380 | if (ops->mode != MTD_OPS_PLACE_OOB && |
| 2381 | ops->mode != MTD_OPS_AUTO_OOB && |
| 2382 | ops->mode != MTD_OPS_RAW) |
| 2383 | return -ENOTSUPP; |
| 2384 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 2385 | nand_get_device(mtd, FL_READING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2387 | if (!ops->datbuf) |
| 2388 | ret = nand_do_read_oob(mtd, from, ops); |
| 2389 | else |
| 2390 | ret = nand_do_read_ops(mtd, from, ops); |
| 2391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | nand_release_device(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2393 | return ret; |
| 2394 | } |
| 2395 | |
| 2396 | |
| 2397 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2398 | * nand_write_page_raw - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2399 | * @mtd: mtd info structure |
| 2400 | * @chip: nand chip info structure |
| 2401 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2402 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2403 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2404 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2405 | * Not for syndrome calculating ECC controllers, which use a special oob layout. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2406 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2407 | static int nand_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2408 | const uint8_t *buf, int oob_required, int page) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2409 | { |
| 2410 | chip->write_buf(mtd, buf, mtd->writesize); |
Brian Norris | 279f08d | 2012-05-02 10:15:03 -0700 | [diff] [blame] | 2411 | if (oob_required) |
| 2412 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2413 | |
| 2414 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | } |
| 2416 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2417 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2418 | * nand_write_page_raw_syndrome - [INTERN] raw page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2419 | * @mtd: mtd info structure |
| 2420 | * @chip: nand chip info structure |
| 2421 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2422 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2423 | * @page: page number to write |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2424 | * |
| 2425 | * We need a special oob layout and handling even when ECC isn't checked. |
| 2426 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2427 | static int nand_write_page_raw_syndrome(struct mtd_info *mtd, |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 2428 | struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2429 | const uint8_t *buf, int oob_required, |
| 2430 | int page) |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2431 | { |
| 2432 | int eccsize = chip->ecc.size; |
| 2433 | int eccbytes = chip->ecc.bytes; |
| 2434 | uint8_t *oob = chip->oob_poi; |
| 2435 | int steps, size; |
| 2436 | |
| 2437 | for (steps = chip->ecc.steps; steps > 0; steps--) { |
| 2438 | chip->write_buf(mtd, buf, eccsize); |
| 2439 | buf += eccsize; |
| 2440 | |
| 2441 | if (chip->ecc.prepad) { |
| 2442 | chip->write_buf(mtd, oob, chip->ecc.prepad); |
| 2443 | oob += chip->ecc.prepad; |
| 2444 | } |
| 2445 | |
Boris BREZILLON | 60c3bc1 | 2014-02-01 19:10:28 +0100 | [diff] [blame] | 2446 | chip->write_buf(mtd, oob, eccbytes); |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2447 | oob += eccbytes; |
| 2448 | |
| 2449 | if (chip->ecc.postpad) { |
| 2450 | chip->write_buf(mtd, oob, chip->ecc.postpad); |
| 2451 | oob += chip->ecc.postpad; |
| 2452 | } |
| 2453 | } |
| 2454 | |
| 2455 | size = mtd->oobsize - (oob - chip->oob_poi); |
| 2456 | if (size) |
| 2457 | chip->write_buf(mtd, oob, size); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2458 | |
| 2459 | return 0; |
David Brownell | 52ff49d | 2009-03-04 12:01:36 -0800 | [diff] [blame] | 2460 | } |
| 2461 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2462 | * nand_write_page_swecc - [REPLACEABLE] software ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2463 | * @mtd: mtd info structure |
| 2464 | * @chip: nand chip info structure |
| 2465 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2466 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2467 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2468 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2469 | static int nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2470 | const uint8_t *buf, int oob_required, |
| 2471 | int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2472 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2473 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2474 | int eccbytes = chip->ecc.bytes; |
| 2475 | int eccsteps = chip->ecc.steps; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 2476 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2477 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2478 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2479 | /* Software ECC calculation */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2480 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) |
| 2481 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2482 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2483 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 2484 | chip->ecc.total); |
| 2485 | if (ret) |
| 2486 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2487 | |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2488 | return chip->ecc.write_page_raw(mtd, chip, buf, 1, page); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2489 | } |
| 2490 | |
| 2491 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2492 | * nand_write_page_hwecc - [REPLACEABLE] hardware ECC based page write function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2493 | * @mtd: mtd info structure |
| 2494 | * @chip: nand chip info structure |
| 2495 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2496 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2497 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2498 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2499 | static int nand_write_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2500 | const uint8_t *buf, int oob_required, |
| 2501 | int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2502 | { |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2503 | int i, eccsize = chip->ecc.size, ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2504 | int eccbytes = chip->ecc.bytes; |
| 2505 | int eccsteps = chip->ecc.steps; |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 2506 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2507 | const uint8_t *p = buf; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2508 | |
| 2509 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2510 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
David Woodhouse | 29da9ce | 2006-05-26 23:05:44 +0100 | [diff] [blame] | 2511 | chip->write_buf(mtd, p, eccsize); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2512 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 2513 | } |
| 2514 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2515 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 2516 | chip->ecc.total); |
| 2517 | if (ret) |
| 2518 | return ret; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2519 | |
| 2520 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2521 | |
| 2522 | return 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2523 | } |
| 2524 | |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2525 | |
| 2526 | /** |
Brian Norris | 73c8aaf | 2015-02-28 02:04:18 -0800 | [diff] [blame] | 2527 | * nand_write_subpage_hwecc - [REPLACEABLE] hardware ECC based subpage write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2528 | * @mtd: mtd info structure |
| 2529 | * @chip: nand chip info structure |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2530 | * @offset: column address of subpage within the page |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2531 | * @data_len: data length |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2532 | * @buf: data buffer |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2533 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2534 | * @page: page number to write |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2535 | */ |
| 2536 | static int nand_write_subpage_hwecc(struct mtd_info *mtd, |
| 2537 | struct nand_chip *chip, uint32_t offset, |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2538 | uint32_t data_len, const uint8_t *buf, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2539 | int oob_required, int page) |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2540 | { |
| 2541 | uint8_t *oob_buf = chip->oob_poi; |
| 2542 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 2543 | int ecc_size = chip->ecc.size; |
| 2544 | int ecc_bytes = chip->ecc.bytes; |
| 2545 | int ecc_steps = chip->ecc.steps; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2546 | uint32_t start_step = offset / ecc_size; |
| 2547 | uint32_t end_step = (offset + data_len - 1) / ecc_size; |
| 2548 | int oob_bytes = mtd->oobsize / ecc_steps; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2549 | int step, ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2550 | |
| 2551 | for (step = 0; step < ecc_steps; step++) { |
| 2552 | /* configure controller for WRITE access */ |
| 2553 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 2554 | |
| 2555 | /* write data (untouched subpages already masked by 0xFF) */ |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2556 | chip->write_buf(mtd, buf, ecc_size); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2557 | |
| 2558 | /* mask ECC of un-touched subpages by padding 0xFF */ |
| 2559 | if ((step < start_step) || (step > end_step)) |
| 2560 | memset(ecc_calc, 0xff, ecc_bytes); |
| 2561 | else |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2562 | chip->ecc.calculate(mtd, buf, ecc_calc); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2563 | |
| 2564 | /* mask OOB of un-touched subpages by padding 0xFF */ |
| 2565 | /* if oob_required, preserve OOB metadata of written subpage */ |
| 2566 | if (!oob_required || (step < start_step) || (step > end_step)) |
| 2567 | memset(oob_buf, 0xff, oob_bytes); |
| 2568 | |
Brian Norris | d6a95080 | 2013-08-08 17:16:36 -0700 | [diff] [blame] | 2569 | buf += ecc_size; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2570 | ecc_calc += ecc_bytes; |
| 2571 | oob_buf += oob_bytes; |
| 2572 | } |
| 2573 | |
| 2574 | /* copy calculated ECC for whole page to chip->buffer->oob */ |
| 2575 | /* this include masked-value(0xFF) for unwritten subpages */ |
| 2576 | ecc_calc = chip->buffers->ecccalc; |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2577 | ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0, |
| 2578 | chip->ecc.total); |
| 2579 | if (ret) |
| 2580 | return ret; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2581 | |
| 2582 | /* write OOB buffer to NAND device */ |
| 2583 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 2584 | |
| 2585 | return 0; |
| 2586 | } |
| 2587 | |
| 2588 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2589 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2590 | * nand_write_page_syndrome - [REPLACEABLE] hardware ECC syndrome based page write |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2591 | * @mtd: mtd info structure |
| 2592 | * @chip: nand chip info structure |
| 2593 | * @buf: data buffer |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2594 | * @oob_required: must write chip->oob_poi to OOB |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2595 | * @page: page number to write |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2596 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2597 | * The hw generator calculates the error syndrome automatically. Therefore we |
| 2598 | * need a special oob layout and handling. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2599 | */ |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2600 | static int nand_write_page_syndrome(struct mtd_info *mtd, |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2601 | struct nand_chip *chip, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2602 | const uint8_t *buf, int oob_required, |
| 2603 | int page) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2604 | { |
| 2605 | int i, eccsize = chip->ecc.size; |
| 2606 | int eccbytes = chip->ecc.bytes; |
| 2607 | int eccsteps = chip->ecc.steps; |
| 2608 | const uint8_t *p = buf; |
| 2609 | uint8_t *oob = chip->oob_poi; |
| 2610 | |
| 2611 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 2612 | |
| 2613 | chip->ecc.hwctl(mtd, NAND_ECC_WRITE); |
| 2614 | chip->write_buf(mtd, p, eccsize); |
| 2615 | |
| 2616 | if (chip->ecc.prepad) { |
| 2617 | chip->write_buf(mtd, oob, chip->ecc.prepad); |
| 2618 | oob += chip->ecc.prepad; |
| 2619 | } |
| 2620 | |
| 2621 | chip->ecc.calculate(mtd, p, oob); |
| 2622 | chip->write_buf(mtd, oob, eccbytes); |
| 2623 | oob += eccbytes; |
| 2624 | |
| 2625 | if (chip->ecc.postpad) { |
| 2626 | chip->write_buf(mtd, oob, chip->ecc.postpad); |
| 2627 | oob += chip->ecc.postpad; |
| 2628 | } |
| 2629 | } |
| 2630 | |
| 2631 | /* Calculate remaining oob bytes */ |
Vitaly Wool | 7e4178f | 2006-06-07 09:34:37 +0400 | [diff] [blame] | 2632 | i = mtd->oobsize - (oob - chip->oob_poi); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2633 | if (i) |
| 2634 | chip->write_buf(mtd, oob, i); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2635 | |
| 2636 | return 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2637 | } |
| 2638 | |
| 2639 | /** |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 2640 | * nand_write_page - [REPLACEABLE] write one page |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2641 | * @mtd: MTD device structure |
| 2642 | * @chip: NAND chip descriptor |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2643 | * @offset: address offset within the page |
| 2644 | * @data_len: length of actual data to be written |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2645 | * @buf: the data to write |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 2646 | * @oob_required: must write chip->oob_poi to OOB |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2647 | * @page: page number to write |
| 2648 | * @cached: cached programming |
| 2649 | * @raw: use _raw version of write_page |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2650 | */ |
| 2651 | 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] | 2652 | uint32_t offset, int data_len, const uint8_t *buf, |
| 2653 | int oob_required, int page, int cached, int raw) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2654 | { |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2655 | int status, subpage; |
| 2656 | |
| 2657 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && |
| 2658 | chip->ecc.write_subpage) |
| 2659 | subpage = offset || (data_len < mtd->writesize); |
| 2660 | else |
| 2661 | subpage = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2662 | |
Marc Gonzalez | 3371d66 | 2016-11-15 10:56:20 +0100 | [diff] [blame] | 2663 | if (nand_standard_page_accessors(&chip->ecc)) |
| 2664 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2665 | |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 2666 | if (unlikely(raw)) |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2667 | status = chip->ecc.write_page_raw(mtd, chip, buf, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2668 | oob_required, page); |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2669 | else if (subpage) |
| 2670 | status = chip->ecc.write_subpage(mtd, chip, offset, data_len, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2671 | buf, oob_required, page); |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 2672 | else |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2673 | status = chip->ecc.write_page(mtd, chip, buf, oob_required, |
| 2674 | page); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 2675 | |
| 2676 | if (status < 0) |
| 2677 | return status; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2678 | |
| 2679 | /* |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2680 | * Cached progamming disabled for now. Not sure if it's worth the |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2681 | * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s). |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2682 | */ |
| 2683 | cached = 0; |
| 2684 | |
Artem Bityutskiy | 3239a6c | 2013-03-04 14:56:18 +0200 | [diff] [blame] | 2685 | if (!cached || !NAND_HAS_CACHEPROG(chip)) { |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2686 | |
Marc Gonzalez | 3371d66 | 2016-11-15 10:56:20 +0100 | [diff] [blame] | 2687 | if (nand_standard_page_accessors(&chip->ecc)) |
| 2688 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2689 | status = chip->waitfunc(mtd, chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2690 | /* |
| 2691 | * See if operation failed and additional status checks are |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2692 | * available. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2693 | */ |
| 2694 | if ((status & NAND_STATUS_FAIL) && (chip->errstat)) |
| 2695 | status = chip->errstat(mtd, chip, FL_WRITING, status, |
| 2696 | page); |
| 2697 | |
| 2698 | if (status & NAND_STATUS_FAIL) |
| 2699 | return -EIO; |
| 2700 | } else { |
| 2701 | chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1); |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 2702 | status = chip->waitfunc(mtd, chip); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2703 | } |
| 2704 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2705 | return 0; |
| 2706 | } |
| 2707 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2708 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2709 | * nand_fill_oob - [INTERN] Transfer client buffer to oob |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2710 | * @mtd: MTD device structure |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2711 | * @oob: oob data buffer |
| 2712 | * @len: oob data write length |
| 2713 | * @ops: oob ops structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2714 | */ |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2715 | static uint8_t *nand_fill_oob(struct mtd_info *mtd, uint8_t *oob, size_t len, |
| 2716 | struct mtd_oob_ops *ops) |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2717 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2718 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2719 | int ret; |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2720 | |
| 2721 | /* |
| 2722 | * Initialise to all 0xFF, to avoid the possibility of left over OOB |
| 2723 | * data from a previous OOB read. |
| 2724 | */ |
| 2725 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
| 2726 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2727 | switch (ops->mode) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2728 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 2729 | case MTD_OPS_PLACE_OOB: |
| 2730 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2731 | memcpy(chip->oob_poi + ops->ooboffs, oob, len); |
| 2732 | return oob + len; |
| 2733 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 2734 | case MTD_OPS_AUTO_OOB: |
| 2735 | ret = mtd_ooblayout_set_databytes(mtd, oob, chip->oob_poi, |
| 2736 | ops->ooboffs, len); |
| 2737 | BUG_ON(ret); |
| 2738 | return oob + len; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2739 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2740 | default: |
| 2741 | BUG(); |
| 2742 | } |
| 2743 | return NULL; |
| 2744 | } |
| 2745 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2746 | #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2747 | |
| 2748 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 2749 | * nand_do_write_ops - [INTERN] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2750 | * @mtd: MTD device structure |
| 2751 | * @to: offset to write to |
| 2752 | * @ops: oob operations description structure |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2753 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2754 | * NAND write with ECC. |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2755 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2756 | static int nand_do_write_ops(struct mtd_info *mtd, loff_t to, |
| 2757 | struct mtd_oob_ops *ops) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2758 | { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2759 | int chipnr, realpage, page, blockmask, column; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2760 | struct nand_chip *chip = mtd_to_nand(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2761 | uint32_t writelen = ops->len; |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2762 | |
| 2763 | uint32_t oobwritelen = ops->ooblen; |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 2764 | uint32_t oobmaxlen = mtd_oobavail(mtd, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2765 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2766 | uint8_t *oob = ops->oobbuf; |
| 2767 | uint8_t *buf = ops->datbuf; |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2768 | int ret; |
Brian Norris | e47f3db | 2012-05-02 10:14:56 -0700 | [diff] [blame] | 2769 | int oob_required = oob ? 1 : 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2770 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2771 | ops->retlen = 0; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2772 | if (!writelen) |
| 2773 | return 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2774 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2775 | /* Reject writes, which are not page aligned */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2776 | if (NOTALIGNED(to) || NOTALIGNED(ops->len)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 2777 | pr_notice("%s: attempt to write non page aligned data\n", |
| 2778 | __func__); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2779 | return -EINVAL; |
| 2780 | } |
| 2781 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2782 | column = to & (mtd->writesize - 1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2783 | |
Thomas Gleixner | 6a93096 | 2006-06-28 00:11:45 +0200 | [diff] [blame] | 2784 | chipnr = (int)(to >> chip->chip_shift); |
| 2785 | chip->select_chip(mtd, chipnr); |
| 2786 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2787 | /* Check, if it is write protected */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2788 | if (nand_check_wp(mtd)) { |
| 2789 | ret = -EIO; |
| 2790 | goto err_out; |
| 2791 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2792 | |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2793 | realpage = (int)(to >> chip->page_shift); |
| 2794 | page = realpage & chip->pagemask; |
| 2795 | blockmask = (1 << (chip->phys_erase_shift - chip->page_shift)) - 1; |
| 2796 | |
| 2797 | /* Invalidate the page cache, when we write to the cached page */ |
Brian Norris | 537ab1b | 2014-07-21 19:08:03 -0700 | [diff] [blame] | 2798 | if (to <= ((loff_t)chip->pagebuf << chip->page_shift) && |
| 2799 | ((loff_t)chip->pagebuf << chip->page_shift) < (to + ops->len)) |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2800 | chip->pagebuf = -1; |
| 2801 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2802 | /* Don't allow multipage oob writes with offset */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2803 | if (oob && ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen)) { |
| 2804 | ret = -EINVAL; |
| 2805 | goto err_out; |
| 2806 | } |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2807 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 2808 | while (1) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2809 | int bytes = mtd->writesize; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2810 | int cached = writelen > bytes && page != blockmask; |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2811 | uint8_t *wbuf = buf; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2812 | int use_bufpoi; |
Hector Palacios | 144f4c9 | 2016-07-18 10:39:18 +0200 | [diff] [blame] | 2813 | int part_pagewr = (column || writelen < mtd->writesize); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2814 | |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2815 | if (part_pagewr) |
| 2816 | use_bufpoi = 1; |
| 2817 | else if (chip->options & NAND_USE_BOUNCE_BUFFER) |
| 2818 | use_bufpoi = !virt_addr_valid(buf); |
| 2819 | else |
| 2820 | use_bufpoi = 0; |
| 2821 | |
| 2822 | /* Partial page write?, or need to use bounce buffer */ |
| 2823 | if (use_bufpoi) { |
| 2824 | pr_debug("%s: using write bounce buffer for buf@%p\n", |
| 2825 | __func__, buf); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2826 | cached = 0; |
Kamal Dasu | 66507c7 | 2014-05-01 20:51:19 -0400 | [diff] [blame] | 2827 | if (part_pagewr) |
| 2828 | bytes = min_t(int, bytes - column, writelen); |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2829 | chip->pagebuf = -1; |
| 2830 | memset(chip->buffers->databuf, 0xff, mtd->writesize); |
| 2831 | memcpy(&chip->buffers->databuf[column], buf, bytes); |
| 2832 | wbuf = chip->buffers->databuf; |
| 2833 | } |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2834 | |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2835 | if (unlikely(oob)) { |
| 2836 | size_t len = min(oobwritelen, oobmaxlen); |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2837 | oob = nand_fill_oob(mtd, oob, len, ops); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2838 | oobwritelen -= len; |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 2839 | } else { |
| 2840 | /* We still need to erase leftover OOB data */ |
| 2841 | memset(chip->oob_poi, 0xff, mtd->oobsize); |
Maxim Levitsky | 782ce79 | 2010-02-22 20:39:36 +0200 | [diff] [blame] | 2842 | } |
Gupta, Pekon | 837a6ba | 2013-03-15 17:55:53 +0530 | [diff] [blame] | 2843 | ret = chip->write_page(mtd, chip, column, bytes, wbuf, |
| 2844 | oob_required, page, cached, |
| 2845 | (ops->mode == MTD_OPS_RAW)); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2846 | if (ret) |
| 2847 | break; |
| 2848 | |
| 2849 | writelen -= bytes; |
| 2850 | if (!writelen) |
| 2851 | break; |
| 2852 | |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 2853 | column = 0; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2854 | buf += bytes; |
| 2855 | realpage++; |
| 2856 | |
| 2857 | page = realpage & chip->pagemask; |
| 2858 | /* Check, if we cross a chip boundary */ |
| 2859 | if (!page) { |
| 2860 | chipnr++; |
| 2861 | chip->select_chip(mtd, -1); |
| 2862 | chip->select_chip(mtd, chipnr); |
| 2863 | } |
| 2864 | } |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2865 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2866 | ops->retlen = ops->len - writelen; |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 2867 | if (unlikely(oob)) |
| 2868 | ops->oobretlen = ops->ooblen; |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2869 | |
| 2870 | err_out: |
| 2871 | chip->select_chip(mtd, -1); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 2872 | return ret; |
| 2873 | } |
| 2874 | |
| 2875 | /** |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2876 | * panic_nand_write - [MTD Interface] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2877 | * @mtd: MTD device structure |
| 2878 | * @to: offset to write to |
| 2879 | * @len: number of bytes to write |
| 2880 | * @retlen: pointer to variable to store the number of written bytes |
| 2881 | * @buf: the data to write |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2882 | * |
| 2883 | * NAND write with ECC. Used when performing writes in interrupt context, this |
| 2884 | * may for example be called by mtdoops when writing an oops while in panic. |
| 2885 | */ |
| 2886 | static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 2887 | size_t *retlen, const uint8_t *buf) |
| 2888 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2889 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2890 | struct mtd_oob_ops ops; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2891 | int ret; |
| 2892 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2893 | /* Wait for the device to get ready */ |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2894 | panic_nand_wait(mtd, chip, 400); |
| 2895 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2896 | /* Grab the device */ |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2897 | panic_nand_get_device(chip, mtd, FL_WRITING); |
| 2898 | |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 2899 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2900 | ops.len = len; |
| 2901 | ops.datbuf = (uint8_t *)buf; |
Huang Shijie | 11041ae6 | 2012-07-03 16:44:14 +0800 | [diff] [blame] | 2902 | ops.mode = MTD_OPS_PLACE_OOB; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2903 | |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2904 | ret = nand_do_write_ops(mtd, to, &ops); |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2905 | |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2906 | *retlen = ops.retlen; |
Simon Kagstrom | 2af7c65 | 2009-10-05 15:55:52 +0200 | [diff] [blame] | 2907 | return ret; |
| 2908 | } |
| 2909 | |
| 2910 | /** |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2911 | * nand_write - [MTD Interface] NAND write with ECC |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2912 | * @mtd: MTD device structure |
| 2913 | * @to: offset to write to |
| 2914 | * @len: number of bytes to write |
| 2915 | * @retlen: pointer to variable to store the number of written bytes |
| 2916 | * @buf: the data to write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2917 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2918 | * NAND write with ECC. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2919 | */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2920 | static int nand_write(struct mtd_info *mtd, loff_t to, size_t len, |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2921 | size_t *retlen, const uint8_t *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2922 | { |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2923 | struct mtd_oob_ops ops; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2924 | int ret; |
| 2925 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 2926 | nand_get_device(mtd, FL_WRITING); |
Brian Norris | 0ec56dc | 2015-02-28 02:02:30 -0800 | [diff] [blame] | 2927 | memset(&ops, 0, sizeof(ops)); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2928 | ops.len = len; |
| 2929 | ops.datbuf = (uint8_t *)buf; |
Huang Shijie | 11041ae6 | 2012-07-03 16:44:14 +0800 | [diff] [blame] | 2930 | ops.mode = MTD_OPS_PLACE_OOB; |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2931 | ret = nand_do_write_ops(mtd, to, &ops); |
Brian Norris | 4a89ff8 | 2011-08-30 18:45:45 -0700 | [diff] [blame] | 2932 | *retlen = ops.retlen; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2933 | nand_release_device(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2934 | return ret; |
| 2935 | } |
| 2936 | |
| 2937 | /** |
| 2938 | * nand_do_write_oob - [MTD Interface] NAND write out-of-band |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2939 | * @mtd: MTD device structure |
| 2940 | * @to: offset to write to |
| 2941 | * @ops: oob operation description structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2942 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 2943 | * NAND write out-of-band. |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2944 | */ |
| 2945 | static int nand_do_write_oob(struct mtd_info *mtd, loff_t to, |
| 2946 | struct mtd_oob_ops *ops) |
| 2947 | { |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2948 | int chipnr, page, status, len; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 2949 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2950 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2951 | pr_debug("%s: to = 0x%08x, len = %i\n", |
vimal singh | 20d8e24 | 2009-07-07 15:49:49 +0530 | [diff] [blame] | 2952 | __func__, (unsigned int)to, (int)ops->ooblen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2953 | |
Boris BREZILLON | 29f1058 | 2016-03-07 10:46:52 +0100 | [diff] [blame] | 2954 | len = mtd_oobavail(mtd, ops); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2955 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2956 | /* Do not allow write past end of page */ |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2957 | if ((ops->ooboffs + ops->ooblen) > len) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2958 | pr_debug("%s: attempt to write past end of page\n", |
| 2959 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2960 | return -EINVAL; |
| 2961 | } |
| 2962 | |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2963 | if (unlikely(ops->ooboffs >= len)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2964 | pr_debug("%s: attempt to start write outside oob\n", |
| 2965 | __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2966 | return -EINVAL; |
| 2967 | } |
| 2968 | |
Jason Liu | 775adc3d4 | 2011-02-25 13:06:18 +0800 | [diff] [blame] | 2969 | /* Do not allow write past end of device */ |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2970 | if (unlikely(to >= mtd->size || |
| 2971 | ops->ooboffs + ops->ooblen > |
| 2972 | ((mtd->size >> chip->page_shift) - |
| 2973 | (to >> chip->page_shift)) * len)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 2974 | pr_debug("%s: attempt to write beyond end of device\n", |
| 2975 | __func__); |
Adrian Hunter | 0373615 | 2007-01-31 17:58:29 +0200 | [diff] [blame] | 2976 | return -EINVAL; |
| 2977 | } |
| 2978 | |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2979 | chipnr = (int)(to >> chip->chip_shift); |
Thomas Gleixner | 7314e9e | 2006-05-25 09:51:54 +0200 | [diff] [blame] | 2980 | |
| 2981 | /* |
| 2982 | * Reset the chip. Some chips (like the Toshiba TC5832DC found in one |
| 2983 | * of my DiskOnChip 2000 test units) will clear the whole data page too |
| 2984 | * if we don't do this. I have no clue why, but I seem to have 'fixed' |
| 2985 | * it in the doc2000 driver in August 1999. dwmw2. |
| 2986 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 2987 | nand_reset(chip, chipnr); |
| 2988 | |
| 2989 | chip->select_chip(mtd, chipnr); |
| 2990 | |
| 2991 | /* Shift to get page */ |
| 2992 | page = (int)(to >> chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2993 | |
| 2994 | /* Check, if it is write protected */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2995 | if (nand_check_wp(mtd)) { |
| 2996 | chip->select_chip(mtd, -1); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 2997 | return -EROFS; |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 2998 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2999 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3000 | /* Invalidate the page cache, if we write to the cached page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3001 | if (page == chip->pagebuf) |
| 3002 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3003 | |
THOMSON, Adam (Adam) | f722013e | 2011-06-14 16:52:38 +0200 | [diff] [blame] | 3004 | nand_fill_oob(mtd, ops->oobbuf, ops->ooblen, ops); |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 3005 | |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3006 | if (ops->mode == MTD_OPS_RAW) |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 3007 | status = chip->ecc.write_oob_raw(mtd, chip, page & chip->pagemask); |
| 3008 | else |
| 3009 | status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3010 | |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3011 | chip->select_chip(mtd, -1); |
| 3012 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3013 | if (status) |
| 3014 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3015 | |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3016 | ops->oobretlen = ops->ooblen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3017 | |
Thomas Gleixner | 7bc3312 | 2006-06-20 20:05:05 +0200 | [diff] [blame] | 3018 | return 0; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3019 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3020 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3021 | /** |
| 3022 | * 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] | 3023 | * @mtd: MTD device structure |
| 3024 | * @to: offset to write to |
| 3025 | * @ops: oob operation description structure |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3026 | */ |
| 3027 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, |
| 3028 | struct mtd_oob_ops *ops) |
| 3029 | { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3030 | int ret = -ENOTSUPP; |
| 3031 | |
| 3032 | ops->retlen = 0; |
| 3033 | |
| 3034 | /* Do not allow writes past end of device */ |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 3035 | if (ops->datbuf && (to + ops->len) > mtd->size) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3036 | pr_debug("%s: attempt to write beyond end of device\n", |
| 3037 | __func__); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3038 | return -EINVAL; |
| 3039 | } |
| 3040 | |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3041 | nand_get_device(mtd, FL_WRITING); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3042 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3043 | switch (ops->mode) { |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 3044 | case MTD_OPS_PLACE_OOB: |
| 3045 | case MTD_OPS_AUTO_OOB: |
| 3046 | case MTD_OPS_RAW: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3047 | break; |
| 3048 | |
| 3049 | default: |
| 3050 | goto out; |
| 3051 | } |
| 3052 | |
| 3053 | if (!ops->datbuf) |
| 3054 | ret = nand_do_write_oob(mtd, to, ops); |
| 3055 | else |
| 3056 | ret = nand_do_write_ops(mtd, to, ops); |
| 3057 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 3058 | out: |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 3059 | nand_release_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3060 | return ret; |
| 3061 | } |
| 3062 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3063 | /** |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 3064 | * single_erase - [GENERIC] NAND standard block erase command function |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3065 | * @mtd: MTD device structure |
| 3066 | * @page: the page address of the block which will be erased |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3067 | * |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 3068 | * Standard erase command for NAND chips. Returns NAND status. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3069 | */ |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 3070 | static int single_erase(struct mtd_info *mtd, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3071 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3072 | struct nand_chip *chip = mtd_to_nand(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3073 | /* Send commands to erase a block */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3074 | chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
| 3075 | chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 3076 | |
| 3077 | return chip->waitfunc(mtd, chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | } |
| 3079 | |
| 3080 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3081 | * nand_erase - [MTD Interface] erase block(s) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3082 | * @mtd: MTD device structure |
| 3083 | * @instr: erase instruction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3084 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3085 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3086 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3087 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3088 | { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3089 | return nand_erase_nand(mtd, instr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3090 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3091 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3093 | * nand_erase_nand - [INTERN] erase block(s) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3094 | * @mtd: MTD device structure |
| 3095 | * @instr: erase instruction |
| 3096 | * @allowbbt: allow erasing the bbt area |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3098 | * Erase one ore more blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3099 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3100 | int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, |
| 3101 | int allowbbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3102 | { |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 3103 | int page, status, pages_per_block, ret, chipnr; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3104 | struct nand_chip *chip = mtd_to_nand(mtd); |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 3105 | loff_t len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3107 | pr_debug("%s: start = 0x%012llx, len = %llu\n", |
| 3108 | __func__, (unsigned long long)instr->addr, |
| 3109 | (unsigned long long)instr->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3110 | |
Vimal Singh | 6fe5a6a | 2010-02-03 14:12:24 +0530 | [diff] [blame] | 3111 | if (check_offs_len(mtd, instr->addr, instr->len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3114 | /* Grab the lock and see if the device is available */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3115 | nand_get_device(mtd, FL_ERASING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3116 | |
| 3117 | /* Shift to get first page */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3118 | page = (int)(instr->addr >> chip->page_shift); |
| 3119 | chipnr = (int)(instr->addr >> chip->chip_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3120 | |
| 3121 | /* Calculate pages in each block */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3122 | pages_per_block = 1 << (chip->phys_erase_shift - chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3123 | |
| 3124 | /* Select the NAND device */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3125 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3127 | /* Check, if it is write protected */ |
| 3128 | if (nand_check_wp(mtd)) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3129 | pr_debug("%s: device is write protected!\n", |
| 3130 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | instr->state = MTD_ERASE_FAILED; |
| 3132 | goto erase_exit; |
| 3133 | } |
| 3134 | |
| 3135 | /* Loop through the pages */ |
| 3136 | len = instr->len; |
| 3137 | |
| 3138 | instr->state = MTD_ERASING; |
| 3139 | |
| 3140 | while (len) { |
Wolfram Sang | 12183a2 | 2011-12-21 23:01:20 +0100 | [diff] [blame] | 3141 | /* 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] | 3142 | if (nand_block_checkbad(mtd, ((loff_t) page) << |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 3143 | chip->page_shift, allowbbt)) { |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 3144 | pr_warn("%s: attempt to erase a bad block at page 0x%08x\n", |
| 3145 | __func__, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | instr->state = MTD_ERASE_FAILED; |
| 3147 | goto erase_exit; |
| 3148 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 3149 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3150 | /* |
| 3151 | * Invalidate the page cache, if we erase the block which |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3152 | * contains the current cached page. |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3153 | */ |
| 3154 | if (page <= chip->pagebuf && chip->pagebuf < |
| 3155 | (page + pages_per_block)) |
| 3156 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3157 | |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 3158 | status = chip->erase(mtd, page & chip->pagemask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3160 | /* |
| 3161 | * See if operation failed and additional status checks are |
| 3162 | * available |
| 3163 | */ |
| 3164 | if ((status & NAND_STATUS_FAIL) && (chip->errstat)) |
| 3165 | status = chip->errstat(mtd, chip, FL_ERASING, |
| 3166 | status, page); |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 3167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3168 | /* See if block erase succeeded */ |
David A. Marlin | a4ab4c5 | 2005-01-23 18:30:53 +0000 | [diff] [blame] | 3169 | if (status & NAND_STATUS_FAIL) { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3170 | pr_debug("%s: failed erase, page 0x%08x\n", |
| 3171 | __func__, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | instr->state = MTD_ERASE_FAILED; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 3173 | instr->fail_addr = |
| 3174 | ((loff_t)page << chip->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3175 | goto erase_exit; |
| 3176 | } |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 3177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | /* Increment page address and decrement length */ |
Dan Carpenter | daae74c | 2013-08-09 12:49:05 +0300 | [diff] [blame] | 3179 | len -= (1ULL << chip->phys_erase_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3180 | page += pages_per_block; |
| 3181 | |
| 3182 | /* Check, if we cross a chip boundary */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3183 | if (len && !(page & chip->pagemask)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3184 | chipnr++; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3185 | chip->select_chip(mtd, -1); |
| 3186 | chip->select_chip(mtd, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3187 | } |
| 3188 | } |
| 3189 | instr->state = MTD_ERASE_DONE; |
| 3190 | |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 3191 | erase_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3192 | |
| 3193 | ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3194 | |
| 3195 | /* Deselect and wake up anyone waiting on the device */ |
Huang Shijie | b0bb690 | 2012-11-19 14:43:29 +0800 | [diff] [blame] | 3196 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3197 | nand_release_device(mtd); |
| 3198 | |
David Woodhouse | 49defc0 | 2007-10-06 15:01:59 -0400 | [diff] [blame] | 3199 | /* Do call back function */ |
| 3200 | if (!ret) |
| 3201 | mtd_erase_callback(instr); |
| 3202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3203 | /* Return more or less happy */ |
| 3204 | return ret; |
| 3205 | } |
| 3206 | |
| 3207 | /** |
| 3208 | * nand_sync - [MTD Interface] sync |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3209 | * @mtd: MTD device structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3210 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3211 | * Sync is actually a wait for chip ready function. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3212 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3213 | static void nand_sync(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3214 | { |
Brian Norris | 289c052 | 2011-07-19 10:06:09 -0700 | [diff] [blame] | 3215 | pr_debug("%s: called\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3216 | |
| 3217 | /* Grab the lock and see if the device is available */ |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3218 | nand_get_device(mtd, FL_SYNCING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3219 | /* Release it and go back */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3220 | nand_release_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | } |
| 3222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3223 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3224 | * nand_block_isbad - [MTD Interface] Check if block at offset is bad |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3225 | * @mtd: MTD device structure |
| 3226 | * @offs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3227 | */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3228 | static int nand_block_isbad(struct mtd_info *mtd, loff_t offs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3229 | { |
Archit Taneja | 9f3e042 | 2016-02-03 14:29:49 +0530 | [diff] [blame] | 3230 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 3231 | int chipnr = (int)(offs >> chip->chip_shift); |
| 3232 | int ret; |
| 3233 | |
| 3234 | /* Select the NAND device */ |
| 3235 | nand_get_device(mtd, FL_READING); |
| 3236 | chip->select_chip(mtd, chipnr); |
| 3237 | |
| 3238 | ret = nand_block_checkbad(mtd, offs, 0); |
| 3239 | |
| 3240 | chip->select_chip(mtd, -1); |
| 3241 | nand_release_device(mtd); |
| 3242 | |
| 3243 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | } |
| 3245 | |
| 3246 | /** |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3247 | * 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] | 3248 | * @mtd: MTD device structure |
| 3249 | * @ofs: offset relative to mtd start |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3250 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3251 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3252 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3253 | int ret; |
| 3254 | |
Florian Fainelli | f8ac041 | 2010-09-07 13:23:43 +0200 | [diff] [blame] | 3255 | ret = nand_block_isbad(mtd, ofs); |
| 3256 | if (ret) { |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3257 | /* If it was bad already, return success and do nothing */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3258 | if (ret > 0) |
| 3259 | return 0; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 3260 | return ret; |
| 3261 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3262 | |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 3263 | return nand_block_markbad_lowlevel(mtd, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | /** |
Zach Brown | 5671842 | 2017-01-10 13:30:20 -0600 | [diff] [blame] | 3267 | * nand_max_bad_blocks - [MTD Interface] Max number of bad blocks for an mtd |
| 3268 | * @mtd: MTD device structure |
| 3269 | * @ofs: offset relative to mtd start |
| 3270 | * @len: length of mtd |
| 3271 | */ |
| 3272 | static int nand_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len) |
| 3273 | { |
| 3274 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 3275 | u32 part_start_block; |
| 3276 | u32 part_end_block; |
| 3277 | u32 part_start_die; |
| 3278 | u32 part_end_die; |
| 3279 | |
| 3280 | /* |
| 3281 | * max_bb_per_die and blocks_per_die used to determine |
| 3282 | * the maximum bad block count. |
| 3283 | */ |
| 3284 | if (!chip->max_bb_per_die || !chip->blocks_per_die) |
| 3285 | return -ENOTSUPP; |
| 3286 | |
| 3287 | /* Get the start and end of the partition in erase blocks. */ |
| 3288 | part_start_block = mtd_div_by_eb(ofs, mtd); |
| 3289 | part_end_block = mtd_div_by_eb(len, mtd) + part_start_block - 1; |
| 3290 | |
| 3291 | /* Get the start and end LUNs of the partition. */ |
| 3292 | part_start_die = part_start_block / chip->blocks_per_die; |
| 3293 | part_end_die = part_end_block / chip->blocks_per_die; |
| 3294 | |
| 3295 | /* |
| 3296 | * Look up the bad blocks per unit and multiply by the number of units |
| 3297 | * that the partition spans. |
| 3298 | */ |
| 3299 | return chip->max_bb_per_die * (part_end_die - part_start_die + 1); |
| 3300 | } |
| 3301 | |
| 3302 | /** |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3303 | * nand_onfi_set_features- [REPLACEABLE] set features for ONFI nand |
| 3304 | * @mtd: MTD device structure |
| 3305 | * @chip: nand chip info structure |
| 3306 | * @addr: feature address. |
| 3307 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 3308 | */ |
| 3309 | static int nand_onfi_set_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 3310 | int addr, uint8_t *subfeature_param) |
| 3311 | { |
| 3312 | int status; |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 3313 | int i; |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3314 | |
David Mosberger | d914c93 | 2013-05-29 15:30:13 +0300 | [diff] [blame] | 3315 | if (!chip->onfi_version || |
| 3316 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 3317 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3318 | return -EINVAL; |
| 3319 | |
| 3320 | chip->cmdfunc(mtd, NAND_CMD_SET_FEATURES, addr, -1); |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 3321 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 3322 | chip->write_byte(mtd, subfeature_param[i]); |
| 3323 | |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3324 | status = chip->waitfunc(mtd, chip); |
| 3325 | if (status & NAND_STATUS_FAIL) |
| 3326 | return -EIO; |
| 3327 | return 0; |
| 3328 | } |
| 3329 | |
| 3330 | /** |
| 3331 | * nand_onfi_get_features- [REPLACEABLE] get features for ONFI nand |
| 3332 | * @mtd: MTD device structure |
| 3333 | * @chip: nand chip info structure |
| 3334 | * @addr: feature address. |
| 3335 | * @subfeature_param: the subfeature parameters, a four bytes array. |
| 3336 | */ |
| 3337 | static int nand_onfi_get_features(struct mtd_info *mtd, struct nand_chip *chip, |
| 3338 | int addr, uint8_t *subfeature_param) |
| 3339 | { |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 3340 | int i; |
| 3341 | |
David Mosberger | d914c93 | 2013-05-29 15:30:13 +0300 | [diff] [blame] | 3342 | if (!chip->onfi_version || |
| 3343 | !(le16_to_cpu(chip->onfi_params.opt_cmd) |
| 3344 | & ONFI_OPT_CMD_SET_GET_FEATURES)) |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3345 | return -EINVAL; |
| 3346 | |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3347 | chip->cmdfunc(mtd, NAND_CMD_GET_FEATURES, addr, -1); |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 3348 | for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i) |
| 3349 | *subfeature_param++ = chip->read_byte(mtd); |
Huang Shijie | 7db03ec | 2012-09-13 14:57:52 +0800 | [diff] [blame] | 3350 | return 0; |
| 3351 | } |
| 3352 | |
| 3353 | /** |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3354 | * nand_suspend - [MTD Interface] Suspend the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3355 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3356 | */ |
| 3357 | static int nand_suspend(struct mtd_info *mtd) |
| 3358 | { |
Huang Shijie | 6a8214a | 2012-11-19 14:43:30 +0800 | [diff] [blame] | 3359 | return nand_get_device(mtd, FL_PM_SUSPENDED); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3360 | } |
| 3361 | |
| 3362 | /** |
| 3363 | * nand_resume - [MTD Interface] Resume the NAND flash |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3364 | * @mtd: MTD device structure |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3365 | */ |
| 3366 | static void nand_resume(struct mtd_info *mtd) |
| 3367 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3368 | struct nand_chip *chip = mtd_to_nand(mtd); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3369 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3370 | if (chip->state == FL_PM_SUSPENDED) |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3371 | nand_release_device(mtd); |
| 3372 | else |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 3373 | pr_err("%s called for a chip which is not in suspended state\n", |
| 3374 | __func__); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 3375 | } |
| 3376 | |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 3377 | /** |
| 3378 | * nand_shutdown - [MTD Interface] Finish the current NAND operation and |
| 3379 | * prevent further operations |
| 3380 | * @mtd: MTD device structure |
| 3381 | */ |
| 3382 | static void nand_shutdown(struct mtd_info *mtd) |
| 3383 | { |
Brian Norris | 9ca641b | 2015-11-09 16:37:28 -0800 | [diff] [blame] | 3384 | nand_get_device(mtd, FL_PM_SUSPENDED); |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 3385 | } |
| 3386 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3387 | /* Set default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3388 | static void nand_set_defaults(struct nand_chip *chip) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3389 | { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3390 | unsigned int busw = chip->options & NAND_BUSWIDTH_16; |
| 3391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3392 | /* check for proper chip_delay setup, set 20us if not */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3393 | if (!chip->chip_delay) |
| 3394 | chip->chip_delay = 20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3395 | |
| 3396 | /* check, if a user supplied command function given */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3397 | if (chip->cmdfunc == NULL) |
| 3398 | chip->cmdfunc = nand_command; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3399 | |
| 3400 | /* check, if a user supplied wait function given */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3401 | if (chip->waitfunc == NULL) |
| 3402 | chip->waitfunc = nand_wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3403 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3404 | if (!chip->select_chip) |
| 3405 | chip->select_chip = nand_select_chip; |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 3406 | |
Huang Shijie | 4204ccc | 2013-08-16 10:10:07 +0800 | [diff] [blame] | 3407 | /* set for ONFI nand */ |
| 3408 | if (!chip->onfi_set_features) |
| 3409 | chip->onfi_set_features = nand_onfi_set_features; |
| 3410 | if (!chip->onfi_get_features) |
| 3411 | chip->onfi_get_features = nand_onfi_get_features; |
| 3412 | |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 3413 | /* If called twice, pointers that depend on busw may need to be reset */ |
| 3414 | if (!chip->read_byte || chip->read_byte == nand_read_byte) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3415 | chip->read_byte = busw ? nand_read_byte16 : nand_read_byte; |
| 3416 | if (!chip->read_word) |
| 3417 | chip->read_word = nand_read_word; |
| 3418 | if (!chip->block_bad) |
| 3419 | chip->block_bad = nand_block_bad; |
| 3420 | if (!chip->block_markbad) |
| 3421 | chip->block_markbad = nand_default_block_markbad; |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 3422 | if (!chip->write_buf || chip->write_buf == nand_write_buf) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3423 | chip->write_buf = busw ? nand_write_buf16 : nand_write_buf; |
Uwe Kleine-König | 05f7835 | 2013-12-05 22:22:04 +0100 | [diff] [blame] | 3424 | if (!chip->write_byte || chip->write_byte == nand_write_byte) |
| 3425 | chip->write_byte = busw ? nand_write_byte16 : nand_write_byte; |
Brian Norris | 68e8078 | 2013-07-18 01:17:02 -0700 | [diff] [blame] | 3426 | if (!chip->read_buf || chip->read_buf == nand_read_buf) |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3427 | chip->read_buf = busw ? nand_read_buf16 : nand_read_buf; |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 3428 | if (!chip->scan_bbt) |
| 3429 | chip->scan_bbt = nand_default_bbt; |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3430 | |
| 3431 | if (!chip->controller) { |
| 3432 | chip->controller = &chip->hwcontrol; |
Marc Gonzalez | d45bc58 | 2016-07-27 11:23:52 +0200 | [diff] [blame] | 3433 | nand_hw_control_init(chip->controller); |
Thomas Gleixner | f75e509 | 2006-05-26 18:52:08 +0200 | [diff] [blame] | 3434 | } |
| 3435 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 3436 | } |
| 3437 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3438 | /* Sanitize ONFI strings so we can safely print them */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3439 | static void sanitize_string(uint8_t *s, size_t len) |
| 3440 | { |
| 3441 | ssize_t i; |
| 3442 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3443 | /* Null terminate */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3444 | s[len - 1] = 0; |
| 3445 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3446 | /* Remove non printable chars */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3447 | for (i = 0; i < len - 1; i++) { |
| 3448 | if (s[i] < ' ' || s[i] > 127) |
| 3449 | s[i] = '?'; |
| 3450 | } |
| 3451 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3452 | /* Remove trailing spaces */ |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3453 | strim(s); |
| 3454 | } |
| 3455 | |
| 3456 | static u16 onfi_crc16(u16 crc, u8 const *p, size_t len) |
| 3457 | { |
| 3458 | int i; |
| 3459 | while (len--) { |
| 3460 | crc ^= *p++ << 8; |
| 3461 | for (i = 0; i < 8; i++) |
| 3462 | crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0); |
| 3463 | } |
| 3464 | |
| 3465 | return crc; |
| 3466 | } |
| 3467 | |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3468 | /* Parse the Extended Parameter Page. */ |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3469 | static int nand_flash_detect_ext_param_page(struct nand_chip *chip, |
| 3470 | struct nand_onfi_params *p) |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3471 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3472 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3473 | struct onfi_ext_param_page *ep; |
| 3474 | struct onfi_ext_section *s; |
| 3475 | struct onfi_ext_ecc_info *ecc; |
| 3476 | uint8_t *cursor; |
| 3477 | int ret = -EINVAL; |
| 3478 | int len; |
| 3479 | int i; |
| 3480 | |
| 3481 | len = le16_to_cpu(p->ext_param_page_length) * 16; |
| 3482 | ep = kmalloc(len, GFP_KERNEL); |
Brian Norris | 5cb1327 | 2013-09-16 17:59:20 -0700 | [diff] [blame] | 3483 | if (!ep) |
| 3484 | return -ENOMEM; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3485 | |
| 3486 | /* Send our own NAND_CMD_PARAM. */ |
| 3487 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1); |
| 3488 | |
| 3489 | /* Use the Change Read Column command to skip the ONFI param pages. */ |
| 3490 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, |
| 3491 | sizeof(*p) * p->num_of_param_pages , -1); |
| 3492 | |
| 3493 | /* Read out the Extended Parameter Page. */ |
| 3494 | chip->read_buf(mtd, (uint8_t *)ep, len); |
| 3495 | if ((onfi_crc16(ONFI_CRC_BASE, ((uint8_t *)ep) + 2, len - 2) |
| 3496 | != le16_to_cpu(ep->crc))) { |
| 3497 | pr_debug("fail in the CRC.\n"); |
| 3498 | goto ext_out; |
| 3499 | } |
| 3500 | |
| 3501 | /* |
| 3502 | * Check the signature. |
| 3503 | * Do not strictly follow the ONFI spec, maybe changed in future. |
| 3504 | */ |
| 3505 | if (strncmp(ep->sig, "EPPS", 4)) { |
| 3506 | pr_debug("The signature is invalid.\n"); |
| 3507 | goto ext_out; |
| 3508 | } |
| 3509 | |
| 3510 | /* find the ECC section. */ |
| 3511 | cursor = (uint8_t *)(ep + 1); |
| 3512 | for (i = 0; i < ONFI_EXT_SECTION_MAX; i++) { |
| 3513 | s = ep->sections + i; |
| 3514 | if (s->type == ONFI_SECTION_TYPE_2) |
| 3515 | break; |
| 3516 | cursor += s->length * 16; |
| 3517 | } |
| 3518 | if (i == ONFI_EXT_SECTION_MAX) { |
| 3519 | pr_debug("We can not find the ECC section.\n"); |
| 3520 | goto ext_out; |
| 3521 | } |
| 3522 | |
| 3523 | /* get the info we want. */ |
| 3524 | ecc = (struct onfi_ext_ecc_info *)cursor; |
| 3525 | |
Brian Norris | 4ae7d22 | 2013-09-16 18:20:21 -0700 | [diff] [blame] | 3526 | if (!ecc->codeword_size) { |
| 3527 | pr_debug("Invalid codeword size\n"); |
| 3528 | goto ext_out; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3529 | } |
| 3530 | |
Brian Norris | 4ae7d22 | 2013-09-16 18:20:21 -0700 | [diff] [blame] | 3531 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 3532 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
Brian Norris | 5cb1327 | 2013-09-16 17:59:20 -0700 | [diff] [blame] | 3533 | ret = 0; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3534 | |
| 3535 | ext_out: |
| 3536 | kfree(ep); |
| 3537 | return ret; |
| 3538 | } |
| 3539 | |
Brian Norris | 8429bb3 | 2013-12-03 15:51:09 -0800 | [diff] [blame] | 3540 | static int nand_setup_read_retry_micron(struct mtd_info *mtd, int retry_mode) |
| 3541 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 3542 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 8429bb3 | 2013-12-03 15:51:09 -0800 | [diff] [blame] | 3543 | uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {retry_mode}; |
| 3544 | |
| 3545 | return chip->onfi_set_features(mtd, chip, ONFI_FEATURE_ADDR_READ_RETRY, |
| 3546 | feature); |
| 3547 | } |
| 3548 | |
| 3549 | /* |
| 3550 | * Configure chip properties from Micron vendor-specific ONFI table |
| 3551 | */ |
| 3552 | static void nand_onfi_detect_micron(struct nand_chip *chip, |
| 3553 | struct nand_onfi_params *p) |
| 3554 | { |
| 3555 | struct nand_onfi_vendor_micron *micron = (void *)p->vendor; |
| 3556 | |
| 3557 | if (le16_to_cpu(p->vendor_revision) < 1) |
| 3558 | return; |
| 3559 | |
| 3560 | chip->read_retries = micron->read_retry_options; |
| 3561 | chip->setup_read_retry = nand_setup_read_retry_micron; |
| 3562 | } |
| 3563 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 3564 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3565 | * 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] | 3566 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3567 | static int nand_flash_detect_onfi(struct nand_chip *chip) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3568 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3569 | struct mtd_info *mtd = nand_to_mtd(chip); |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3570 | struct nand_onfi_params *p = &chip->onfi_params; |
Brian Norris | bd9c6e9 | 2013-11-29 22:04:28 -0800 | [diff] [blame] | 3571 | int i, j; |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3572 | int val; |
| 3573 | |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 3574 | /* Try ONFI for unknown chip or LP */ |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3575 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x20, -1); |
| 3576 | if (chip->read_byte(mtd) != 'O' || chip->read_byte(mtd) != 'N' || |
| 3577 | chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I') |
| 3578 | return 0; |
| 3579 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3580 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1); |
| 3581 | for (i = 0; i < 3; i++) { |
Brian Norris | bd9c6e9 | 2013-11-29 22:04:28 -0800 | [diff] [blame] | 3582 | for (j = 0; j < sizeof(*p); j++) |
| 3583 | ((uint8_t *)p)[j] = chip->read_byte(mtd); |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3584 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) == |
| 3585 | le16_to_cpu(p->crc)) { |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3586 | break; |
| 3587 | } |
| 3588 | } |
| 3589 | |
Brian Norris | c7f23a7 | 2013-08-13 10:51:55 -0700 | [diff] [blame] | 3590 | if (i == 3) { |
| 3591 | pr_err("Could not find valid ONFI parameter page; aborting\n"); |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3592 | return 0; |
Brian Norris | c7f23a7 | 2013-08-13 10:51:55 -0700 | [diff] [blame] | 3593 | } |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3594 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 3595 | /* Check version */ |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3596 | val = le16_to_cpu(p->revision); |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 3597 | if (val & (1 << 5)) |
| 3598 | chip->onfi_version = 23; |
| 3599 | else if (val & (1 << 4)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3600 | chip->onfi_version = 22; |
| 3601 | else if (val & (1 << 3)) |
| 3602 | chip->onfi_version = 21; |
| 3603 | else if (val & (1 << 2)) |
| 3604 | chip->onfi_version = 20; |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 3605 | else if (val & (1 << 1)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3606 | chip->onfi_version = 10; |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 3607 | |
| 3608 | if (!chip->onfi_version) { |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 3609 | pr_info("unsupported ONFI version: %d\n", val); |
Brian Norris | b7b1a29 | 2010-12-12 00:23:33 -0800 | [diff] [blame] | 3610 | return 0; |
| 3611 | } |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3612 | |
| 3613 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 3614 | sanitize_string(p->model, sizeof(p->model)); |
| 3615 | if (!mtd->name) |
| 3616 | mtd->name = p->model; |
Brian Norris | 4355b70 | 2013-08-27 18:45:10 -0700 | [diff] [blame] | 3617 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3618 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
Brian Norris | 4355b70 | 2013-08-27 18:45:10 -0700 | [diff] [blame] | 3619 | |
| 3620 | /* |
| 3621 | * pages_per_block and blocks_per_lun may not be a power-of-2 size |
| 3622 | * (don't ask me who thought of this...). MTD assumes that these |
| 3623 | * dimensions will be power-of-2, so just truncate the remaining area. |
| 3624 | */ |
| 3625 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 3626 | mtd->erasesize *= mtd->writesize; |
| 3627 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3628 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
Brian Norris | 4355b70 | 2013-08-27 18:45:10 -0700 | [diff] [blame] | 3629 | |
| 3630 | /* See erasesize comment */ |
| 3631 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
Matthieu CASTET | 6379575 | 2012-03-19 15:35:25 +0100 | [diff] [blame] | 3632 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
Huang Shijie | 13fbd17 | 2013-09-25 14:58:13 +0800 | [diff] [blame] | 3633 | chip->bits_per_cell = p->bits_per_cell; |
Huang Shijie | e2985fc | 2013-05-17 11:17:30 +0800 | [diff] [blame] | 3634 | |
Zach Brown | 34da5f5 | 2017-01-10 13:30:21 -0600 | [diff] [blame] | 3635 | chip->max_bb_per_die = le16_to_cpu(p->bb_per_lun); |
| 3636 | chip->blocks_per_die = le32_to_cpu(p->blocks_per_lun); |
| 3637 | |
Huang Shijie | e2985fc | 2013-05-17 11:17:30 +0800 | [diff] [blame] | 3638 | if (onfi_feature(chip) & ONFI_FEATURE_16_BIT_BUS) |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3639 | chip->options |= NAND_BUSWIDTH_16; |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3640 | |
Huang Shijie | 10c86ba | 2013-05-17 11:17:26 +0800 | [diff] [blame] | 3641 | if (p->ecc_bits != 0xff) { |
| 3642 | chip->ecc_strength_ds = p->ecc_bits; |
| 3643 | chip->ecc_step_ds = 512; |
Huang Shijie | 6dcbe0c | 2013-05-22 10:28:27 +0800 | [diff] [blame] | 3644 | } else if (chip->onfi_version >= 21 && |
| 3645 | (onfi_feature(chip) & ONFI_FEATURE_EXT_PARAM_PAGE)) { |
| 3646 | |
| 3647 | /* |
| 3648 | * The nand_flash_detect_ext_param_page() uses the |
| 3649 | * Change Read Column command which maybe not supported |
| 3650 | * by the chip->cmdfunc. So try to update the chip->cmdfunc |
| 3651 | * now. We do not replace user supplied command function. |
| 3652 | */ |
| 3653 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 3654 | chip->cmdfunc = nand_command_lp; |
| 3655 | |
| 3656 | /* The Extended Parameter Page is supported since ONFI 2.1. */ |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3657 | if (nand_flash_detect_ext_param_page(chip, p)) |
Brian Norris | c7f23a7 | 2013-08-13 10:51:55 -0700 | [diff] [blame] | 3658 | pr_warn("Failed to detect ONFI extended param page\n"); |
| 3659 | } else { |
| 3660 | pr_warn("Could not retrieve ONFI ECC requirements\n"); |
Huang Shijie | 10c86ba | 2013-05-17 11:17:26 +0800 | [diff] [blame] | 3661 | } |
| 3662 | |
Brian Norris | 8429bb3 | 2013-12-03 15:51:09 -0800 | [diff] [blame] | 3663 | if (p->jedec_id == NAND_MFR_MICRON) |
| 3664 | nand_onfi_detect_micron(chip, p); |
| 3665 | |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 3666 | return 1; |
| 3667 | } |
| 3668 | |
| 3669 | /* |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 3670 | * Check if the NAND chip is JEDEC compliant, returns 1 if it is, 0 otherwise. |
| 3671 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3672 | static int nand_flash_detect_jedec(struct nand_chip *chip) |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 3673 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3674 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 3675 | struct nand_jedec_params *p = &chip->jedec_params; |
| 3676 | struct jedec_ecc_info *ecc; |
| 3677 | int val; |
| 3678 | int i, j; |
| 3679 | |
| 3680 | /* Try JEDEC for unknown chip or LP */ |
| 3681 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x40, -1); |
| 3682 | if (chip->read_byte(mtd) != 'J' || chip->read_byte(mtd) != 'E' || |
| 3683 | chip->read_byte(mtd) != 'D' || chip->read_byte(mtd) != 'E' || |
| 3684 | chip->read_byte(mtd) != 'C') |
| 3685 | return 0; |
| 3686 | |
| 3687 | chip->cmdfunc(mtd, NAND_CMD_PARAM, 0x40, -1); |
| 3688 | for (i = 0; i < 3; i++) { |
| 3689 | for (j = 0; j < sizeof(*p); j++) |
| 3690 | ((uint8_t *)p)[j] = chip->read_byte(mtd); |
| 3691 | |
| 3692 | if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 510) == |
| 3693 | le16_to_cpu(p->crc)) |
| 3694 | break; |
| 3695 | } |
| 3696 | |
| 3697 | if (i == 3) { |
| 3698 | pr_err("Could not find valid JEDEC parameter page; aborting\n"); |
| 3699 | return 0; |
| 3700 | } |
| 3701 | |
| 3702 | /* Check version */ |
| 3703 | val = le16_to_cpu(p->revision); |
| 3704 | if (val & (1 << 2)) |
| 3705 | chip->jedec_version = 10; |
| 3706 | else if (val & (1 << 1)) |
| 3707 | chip->jedec_version = 1; /* vendor specific version */ |
| 3708 | |
| 3709 | if (!chip->jedec_version) { |
| 3710 | pr_info("unsupported JEDEC version: %d\n", val); |
| 3711 | return 0; |
| 3712 | } |
| 3713 | |
| 3714 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 3715 | sanitize_string(p->model, sizeof(p->model)); |
| 3716 | if (!mtd->name) |
| 3717 | mtd->name = p->model; |
| 3718 | |
| 3719 | mtd->writesize = le32_to_cpu(p->byte_per_page); |
| 3720 | |
| 3721 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 3722 | mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1); |
| 3723 | mtd->erasesize *= mtd->writesize; |
| 3724 | |
| 3725 | mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page); |
| 3726 | |
| 3727 | /* Please reference to the comment for nand_flash_detect_onfi. */ |
| 3728 | chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1); |
| 3729 | chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count; |
| 3730 | chip->bits_per_cell = p->bits_per_cell; |
| 3731 | |
| 3732 | if (jedec_feature(chip) & JEDEC_FEATURE_16_BIT_BUS) |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3733 | chip->options |= NAND_BUSWIDTH_16; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 3734 | |
| 3735 | /* ECC info */ |
| 3736 | ecc = &p->ecc_info[0]; |
| 3737 | |
| 3738 | if (ecc->codeword_size >= 9) { |
| 3739 | chip->ecc_strength_ds = ecc->ecc_bits; |
| 3740 | chip->ecc_step_ds = 1 << ecc->codeword_size; |
| 3741 | } else { |
| 3742 | pr_warn("Invalid codeword size\n"); |
| 3743 | } |
| 3744 | |
| 3745 | return 1; |
| 3746 | } |
| 3747 | |
| 3748 | /* |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 3749 | * nand_id_has_period - Check if an ID string has a given wraparound period |
| 3750 | * @id_data: the ID string |
| 3751 | * @arrlen: the length of the @id_data array |
| 3752 | * @period: the period of repitition |
| 3753 | * |
| 3754 | * Check if an ID string is repeated within a given sequence of bytes at |
| 3755 | * specific repetition interval period (e.g., {0x20,0x01,0x7F,0x20} has a |
Brian Norris | d4d4f1b | 2012-11-14 21:54:20 -0800 | [diff] [blame] | 3756 | * 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] | 3757 | * if the repetition has a period of @period; otherwise, returns zero. |
| 3758 | */ |
| 3759 | static int nand_id_has_period(u8 *id_data, int arrlen, int period) |
| 3760 | { |
| 3761 | int i, j; |
| 3762 | for (i = 0; i < period; i++) |
| 3763 | for (j = i + period; j < arrlen; j += period) |
| 3764 | if (id_data[i] != id_data[j]) |
| 3765 | return 0; |
| 3766 | return 1; |
| 3767 | } |
| 3768 | |
| 3769 | /* |
| 3770 | * nand_id_len - Get the length of an ID string returned by CMD_READID |
| 3771 | * @id_data: the ID string |
| 3772 | * @arrlen: the length of the @id_data array |
| 3773 | |
| 3774 | * Returns the length of the ID string, according to known wraparound/trailing |
| 3775 | * zero patterns. If no pattern exists, returns the length of the array. |
| 3776 | */ |
| 3777 | static int nand_id_len(u8 *id_data, int arrlen) |
| 3778 | { |
| 3779 | int last_nonzero, period; |
| 3780 | |
| 3781 | /* Find last non-zero byte */ |
| 3782 | for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--) |
| 3783 | if (id_data[last_nonzero]) |
| 3784 | break; |
| 3785 | |
| 3786 | /* All zeros */ |
| 3787 | if (last_nonzero < 0) |
| 3788 | return 0; |
| 3789 | |
| 3790 | /* Calculate wraparound period */ |
| 3791 | for (period = 1; period < arrlen; period++) |
| 3792 | if (nand_id_has_period(id_data, arrlen, period)) |
| 3793 | break; |
| 3794 | |
| 3795 | /* There's a repeated pattern */ |
| 3796 | if (period < arrlen) |
| 3797 | return period; |
| 3798 | |
| 3799 | /* There are trailing zeros */ |
| 3800 | if (last_nonzero < arrlen - 1) |
| 3801 | return last_nonzero + 1; |
| 3802 | |
| 3803 | /* No pattern detected */ |
| 3804 | return arrlen; |
| 3805 | } |
| 3806 | |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 3807 | /* Extract the bits of per cell from the 3rd byte of the extended ID */ |
| 3808 | static int nand_get_bits_per_cell(u8 cellinfo) |
| 3809 | { |
| 3810 | int bits; |
| 3811 | |
| 3812 | bits = cellinfo & NAND_CI_CELLTYPE_MSK; |
| 3813 | bits >>= NAND_CI_CELLTYPE_SHIFT; |
| 3814 | return bits + 1; |
| 3815 | } |
| 3816 | |
Brian Norris | e3b88bd | 2012-09-24 20:40:52 -0700 | [diff] [blame] | 3817 | /* |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3818 | * Many new NAND share similar device ID codes, which represent the size of the |
| 3819 | * chip. The rest of the parameters must be decoded according to generic or |
| 3820 | * manufacturer-specific "extended ID" decoding patterns. |
| 3821 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3822 | static void nand_decode_ext_id(struct nand_chip *chip) |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3823 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3824 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3825 | int extid, id_len = chip->id.len; |
| 3826 | u8 *id_data = chip->id.data; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3827 | /* The 3rd id byte holds MLC / multichip data */ |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 3828 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3829 | /* The 4th id byte is the important one */ |
| 3830 | extid = id_data[3]; |
| 3831 | |
| 3832 | /* |
| 3833 | * Field definitions are in the following datasheets: |
| 3834 | * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32) |
Brian Norris | af451af | 2012-10-09 23:26:06 -0700 | [diff] [blame] | 3835 | * New Samsung (6 byte ID): Samsung K9GAG08U0F (p.44) |
Brian Norris | 73ca392 | 2012-09-24 20:40:54 -0700 | [diff] [blame] | 3836 | * Hynix MLC (6 byte ID): Hynix H27UBG8T2B (p.22) |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3837 | * |
Brian Norris | af451af | 2012-10-09 23:26:06 -0700 | [diff] [blame] | 3838 | * Check for ID length, non-zero 6th byte, cell type, and Hynix/Samsung |
| 3839 | * ID to decide what to do. |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3840 | */ |
Brian Norris | af451af | 2012-10-09 23:26:06 -0700 | [diff] [blame] | 3841 | if (id_len == 6 && id_data[0] == NAND_MFR_SAMSUNG && |
Huang Shijie | 1d0ed69 | 2013-09-25 14:58:10 +0800 | [diff] [blame] | 3842 | !nand_is_slc(chip) && id_data[5] != 0x00) { |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3843 | /* Calc pagesize */ |
| 3844 | mtd->writesize = 2048 << (extid & 0x03); |
| 3845 | extid >>= 2; |
| 3846 | /* Calc oobsize */ |
Brian Norris | e2d3a35 | 2012-09-24 20:40:55 -0700 | [diff] [blame] | 3847 | switch (((extid >> 2) & 0x04) | (extid & 0x03)) { |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3848 | case 1: |
| 3849 | mtd->oobsize = 128; |
| 3850 | break; |
| 3851 | case 2: |
| 3852 | mtd->oobsize = 218; |
| 3853 | break; |
| 3854 | case 3: |
| 3855 | mtd->oobsize = 400; |
| 3856 | break; |
Brian Norris | e2d3a35 | 2012-09-24 20:40:55 -0700 | [diff] [blame] | 3857 | case 4: |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3858 | mtd->oobsize = 436; |
| 3859 | break; |
Brian Norris | e2d3a35 | 2012-09-24 20:40:55 -0700 | [diff] [blame] | 3860 | case 5: |
| 3861 | mtd->oobsize = 512; |
| 3862 | break; |
| 3863 | case 6: |
Brian Norris | e2d3a35 | 2012-09-24 20:40:55 -0700 | [diff] [blame] | 3864 | mtd->oobsize = 640; |
| 3865 | break; |
Huang Shijie | 94d04e8 | 2013-12-25 17:18:55 +0800 | [diff] [blame] | 3866 | case 7: |
| 3867 | default: /* Other cases are "reserved" (unknown) */ |
| 3868 | mtd->oobsize = 1024; |
| 3869 | break; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3870 | } |
| 3871 | extid >>= 2; |
| 3872 | /* Calc blocksize */ |
| 3873 | mtd->erasesize = (128 * 1024) << |
| 3874 | (((extid >> 1) & 0x04) | (extid & 0x03)); |
Brian Norris | 73ca392 | 2012-09-24 20:40:54 -0700 | [diff] [blame] | 3875 | } else if (id_len == 6 && id_data[0] == NAND_MFR_HYNIX && |
Huang Shijie | 1d0ed69 | 2013-09-25 14:58:10 +0800 | [diff] [blame] | 3876 | !nand_is_slc(chip)) { |
Brian Norris | 73ca392 | 2012-09-24 20:40:54 -0700 | [diff] [blame] | 3877 | unsigned int tmp; |
| 3878 | |
| 3879 | /* Calc pagesize */ |
| 3880 | mtd->writesize = 2048 << (extid & 0x03); |
| 3881 | extid >>= 2; |
| 3882 | /* Calc oobsize */ |
| 3883 | switch (((extid >> 2) & 0x04) | (extid & 0x03)) { |
| 3884 | case 0: |
| 3885 | mtd->oobsize = 128; |
| 3886 | break; |
| 3887 | case 1: |
| 3888 | mtd->oobsize = 224; |
| 3889 | break; |
| 3890 | case 2: |
| 3891 | mtd->oobsize = 448; |
| 3892 | break; |
| 3893 | case 3: |
| 3894 | mtd->oobsize = 64; |
| 3895 | break; |
| 3896 | case 4: |
| 3897 | mtd->oobsize = 32; |
| 3898 | break; |
| 3899 | case 5: |
| 3900 | mtd->oobsize = 16; |
| 3901 | break; |
| 3902 | default: |
| 3903 | mtd->oobsize = 640; |
| 3904 | break; |
| 3905 | } |
| 3906 | extid >>= 2; |
| 3907 | /* Calc blocksize */ |
| 3908 | tmp = ((extid >> 1) & 0x04) | (extid & 0x03); |
| 3909 | if (tmp < 0x03) |
| 3910 | mtd->erasesize = (128 * 1024) << tmp; |
| 3911 | else if (tmp == 0x03) |
| 3912 | mtd->erasesize = 768 * 1024; |
| 3913 | else |
| 3914 | mtd->erasesize = (64 * 1024) << tmp; |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3915 | } else { |
| 3916 | /* Calc pagesize */ |
| 3917 | mtd->writesize = 1024 << (extid & 0x03); |
| 3918 | extid >>= 2; |
| 3919 | /* Calc oobsize */ |
| 3920 | mtd->oobsize = (8 << (extid & 0x01)) * |
| 3921 | (mtd->writesize >> 9); |
| 3922 | extid >>= 2; |
| 3923 | /* Calc blocksize. Blocksize is multiples of 64KiB */ |
| 3924 | mtd->erasesize = (64 * 1024) << (extid & 0x03); |
| 3925 | extid >>= 2; |
| 3926 | /* Get buswidth information */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3927 | if (extid & 0x1) |
| 3928 | chip->options |= NAND_BUSWIDTH_16; |
Brian Norris | 60c6738 | 2013-06-25 13:17:59 -0700 | [diff] [blame] | 3929 | |
| 3930 | /* |
| 3931 | * Toshiba 24nm raw SLC (i.e., not BENAND) have 32B OOB per |
| 3932 | * 512B page. For Toshiba SLC, we decode the 5th/6th byte as |
| 3933 | * follows: |
| 3934 | * - ID byte 6, bits[2:0]: 100b -> 43nm, 101b -> 32nm, |
| 3935 | * 110b -> 24nm |
| 3936 | * - ID byte 5, bit[7]: 1 -> BENAND, 0 -> raw SLC |
| 3937 | */ |
| 3938 | if (id_len >= 6 && id_data[0] == NAND_MFR_TOSHIBA && |
Huang Shijie | 1d0ed69 | 2013-09-25 14:58:10 +0800 | [diff] [blame] | 3939 | nand_is_slc(chip) && |
Brian Norris | 60c6738 | 2013-06-25 13:17:59 -0700 | [diff] [blame] | 3940 | (id_data[5] & 0x7) == 0x6 /* 24nm */ && |
| 3941 | !(id_data[4] & 0x80) /* !BENAND */) { |
| 3942 | mtd->oobsize = 32 * mtd->writesize >> 9; |
| 3943 | } |
| 3944 | |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 3945 | } |
| 3946 | } |
| 3947 | |
| 3948 | /* |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 3949 | * Old devices have chip data hardcoded in the device ID table. nand_decode_id |
| 3950 | * decodes a matching ID table entry and assigns the MTD size parameters for |
| 3951 | * the chip. |
| 3952 | */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 3953 | 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] | 3954 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3955 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3956 | u8 *id_data = chip->id.data; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 3957 | int maf_id = id_data[0]; |
| 3958 | |
| 3959 | mtd->erasesize = type->erasesize; |
| 3960 | mtd->writesize = type->pagesize; |
| 3961 | mtd->oobsize = mtd->writesize / 32; |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 3962 | |
Huang Shijie | 1c195e9 | 2013-09-25 14:58:12 +0800 | [diff] [blame] | 3963 | /* All legacy ID NAND are small-page, SLC */ |
| 3964 | chip->bits_per_cell = 1; |
| 3965 | |
Brian Norris | f23a481 | 2012-09-24 20:40:51 -0700 | [diff] [blame] | 3966 | /* |
| 3967 | * Check for Spansion/AMD ID + repeating 5th, 6th byte since |
| 3968 | * some Spansion chips have erasesize that conflicts with size |
| 3969 | * listed in nand_ids table. |
| 3970 | * Data sheet (5 byte ID): Spansion S30ML-P ORNAND (p.39) |
| 3971 | */ |
| 3972 | if (maf_id == NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00 |
| 3973 | && id_data[6] == 0x00 && id_data[7] == 0x00 |
| 3974 | && mtd->writesize == 512) { |
| 3975 | mtd->erasesize = 128 * 1024; |
| 3976 | mtd->erasesize <<= ((id_data[3] & 0x03) << 1); |
| 3977 | } |
| 3978 | } |
| 3979 | |
| 3980 | /* |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 3981 | * Set the bad block marker/indicator (BBM/BBI) patterns according to some |
| 3982 | * heuristic patterns using various detected parameters (e.g., manufacturer, |
| 3983 | * page size, cell-type information). |
| 3984 | */ |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3985 | static void nand_decode_bbm_options(struct nand_chip *chip) |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 3986 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 3987 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 3988 | u8 *id_data = chip->id.data; |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 3989 | int maf_id = id_data[0]; |
| 3990 | |
| 3991 | /* Set the bad block position */ |
| 3992 | if (mtd->writesize > 512 || (chip->options & NAND_BUSWIDTH_16)) |
| 3993 | chip->badblockpos = NAND_LARGE_BADBLOCK_POS; |
| 3994 | else |
| 3995 | chip->badblockpos = NAND_SMALL_BADBLOCK_POS; |
| 3996 | |
| 3997 | /* |
| 3998 | * Bad block marker is stored in the last page of each block on Samsung |
| 3999 | * and Hynix MLC devices; stored in first two pages of each block on |
| 4000 | * Micron devices with 2KiB pages and on SLC Samsung, Hynix, Toshiba, |
| 4001 | * AMD/Spansion, and Macronix. All others scan only the first page. |
| 4002 | */ |
Huang Shijie | 1d0ed69 | 2013-09-25 14:58:10 +0800 | [diff] [blame] | 4003 | if (!nand_is_slc(chip) && |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4004 | (maf_id == NAND_MFR_SAMSUNG || |
| 4005 | maf_id == NAND_MFR_HYNIX)) |
| 4006 | chip->bbt_options |= NAND_BBT_SCANLASTPAGE; |
Huang Shijie | 1d0ed69 | 2013-09-25 14:58:10 +0800 | [diff] [blame] | 4007 | else if ((nand_is_slc(chip) && |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4008 | (maf_id == NAND_MFR_SAMSUNG || |
| 4009 | maf_id == NAND_MFR_HYNIX || |
| 4010 | maf_id == NAND_MFR_TOSHIBA || |
| 4011 | maf_id == NAND_MFR_AMD || |
| 4012 | maf_id == NAND_MFR_MACRONIX)) || |
| 4013 | (mtd->writesize == 2048 && |
| 4014 | maf_id == NAND_MFR_MICRON)) |
| 4015 | chip->bbt_options |= NAND_BBT_SCAN2NDPAGE; |
| 4016 | } |
| 4017 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4018 | static inline bool is_full_id_nand(struct nand_flash_dev *type) |
| 4019 | { |
| 4020 | return type->id_len; |
| 4021 | } |
| 4022 | |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4023 | static bool find_full_id_nand(struct nand_chip *chip, |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4024 | struct nand_flash_dev *type) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4025 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4026 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4027 | u8 *id_data = chip->id.data; |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4028 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4029 | if (!strncmp(type->id, id_data, type->id_len)) { |
| 4030 | mtd->writesize = type->pagesize; |
| 4031 | mtd->erasesize = type->erasesize; |
| 4032 | mtd->oobsize = type->oobsize; |
| 4033 | |
Huang Shijie | 7db906b | 2013-09-25 14:58:11 +0800 | [diff] [blame] | 4034 | chip->bits_per_cell = nand_get_bits_per_cell(id_data[2]); |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4035 | chip->chipsize = (uint64_t)type->chipsize << 20; |
| 4036 | chip->options |= type->options; |
Huang Shijie | 5721934 | 2013-05-17 11:17:32 +0800 | [diff] [blame] | 4037 | chip->ecc_strength_ds = NAND_ECC_STRENGTH(type); |
| 4038 | chip->ecc_step_ds = NAND_ECC_STEP(type); |
Boris BREZILLON | 57a94e2 | 2014-09-22 20:11:50 +0200 | [diff] [blame] | 4039 | chip->onfi_timing_mode_default = |
| 4040 | type->onfi_timing_mode_default; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4041 | |
Cai Zhiyong | 092b6a1 | 2013-12-25 21:19:21 +0800 | [diff] [blame] | 4042 | if (!mtd->name) |
| 4043 | mtd->name = type->name; |
| 4044 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4045 | return true; |
| 4046 | } |
| 4047 | return false; |
| 4048 | } |
| 4049 | |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4050 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4051 | * 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] | 4052 | */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame^] | 4053 | 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] | 4054 | { |
Boris Brezillon | cbe435a | 2016-05-24 16:56:22 +0200 | [diff] [blame] | 4055 | struct mtd_info *mtd = nand_to_mtd(chip); |
Cai Zhiyong | bb77082 | 2013-12-25 20:11:15 +0800 | [diff] [blame] | 4056 | int busw; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4057 | int i, maf_idx; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4058 | u8 *id_data = chip->id.data; |
| 4059 | u8 maf_id, dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4060 | |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4061 | /* |
| 4062 | * Reset the chip, required by some chips (e.g. Micron MT29FxGxxxxx) |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4063 | * after power-up. |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4064 | */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4065 | nand_reset(chip, 0); |
| 4066 | |
| 4067 | /* Select the device */ |
| 4068 | chip->select_chip(mtd, 0); |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4069 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4070 | /* Send the command for reading device ID */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4071 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4072 | |
| 4073 | /* Read manufacturer and device IDs */ |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4074 | maf_id = chip->read_byte(mtd); |
| 4075 | dev_id = chip->read_byte(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4076 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4077 | /* |
| 4078 | * 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] | 4079 | * interface concerns can cause random data which looks like a |
| 4080 | * possibly credible NAND flash to appear. If the two results do |
| 4081 | * not match, ignore the device completely. |
| 4082 | */ |
| 4083 | |
| 4084 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
| 4085 | |
Brian Norris | 4aef9b7 | 2012-09-24 20:40:48 -0700 | [diff] [blame] | 4086 | /* Read entire ID string */ |
| 4087 | for (i = 0; i < 8; i++) |
Kevin Cernekee | 426c457 | 2010-05-04 20:58:03 -0700 | [diff] [blame] | 4088 | id_data[i] = chip->read_byte(mtd); |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 4089 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4090 | if (id_data[0] != maf_id || id_data[1] != dev_id) { |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4091 | 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] | 4092 | maf_id, dev_id, id_data[0], id_data[1]); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4093 | return -ENODEV; |
Ben Dooks | ed8165c | 2008-04-14 14:58:58 +0100 | [diff] [blame] | 4094 | } |
| 4095 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4096 | chip->id.len = nand_id_len(id_data, 8); |
| 4097 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4098 | if (!type) |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4099 | type = nand_flash_ids; |
| 4100 | |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4101 | /* |
| 4102 | * Save the NAND_BUSWIDTH_16 flag before letting auto-detection logic |
| 4103 | * override it. |
| 4104 | * This is required to make sure initial NAND bus width set by the |
| 4105 | * NAND controller driver is coherent with the real NAND bus width |
| 4106 | * (extracted by auto-detection code). |
| 4107 | */ |
| 4108 | busw = chip->options & NAND_BUSWIDTH_16; |
| 4109 | |
| 4110 | /* |
| 4111 | * The flag is only set (never cleared), reset it to its default value |
| 4112 | * before starting auto-detection. |
| 4113 | */ |
| 4114 | chip->options &= ~NAND_BUSWIDTH_16; |
| 4115 | |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4116 | for (; type->name != NULL; type++) { |
| 4117 | if (is_full_id_nand(type)) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4118 | if (find_full_id_nand(chip, type)) |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4119 | goto ident_done; |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4120 | } else if (dev_id == type->dev_id) { |
Brian Norris | db5b09f | 2015-05-22 10:43:12 -0700 | [diff] [blame] | 4121 | break; |
Huang Shijie | ec6e87e | 2013-03-15 11:01:00 +0800 | [diff] [blame] | 4122 | } |
| 4123 | } |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4124 | |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4125 | chip->onfi_version = 0; |
| 4126 | if (!type->name || !type->pagesize) { |
Masahiro Yamada | 35fc519 | 2014-04-09 16:26:26 +0900 | [diff] [blame] | 4127 | /* Check if the chip is ONFI compliant */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4128 | if (nand_flash_detect_onfi(chip)) |
Florian Fainelli | 6fb277b | 2010-09-01 22:28:59 +0200 | [diff] [blame] | 4129 | goto ident_done; |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4130 | |
| 4131 | /* Check if the chip is JEDEC compliant */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4132 | if (nand_flash_detect_jedec(chip)) |
Huang Shijie | 9136181 | 2014-02-21 13:39:40 +0800 | [diff] [blame] | 4133 | goto ident_done; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4134 | } |
| 4135 | |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4136 | if (!type->name) |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4137 | return -ENODEV; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4138 | |
Thomas Gleixner | ba0251f | 2006-05-27 01:02:13 +0200 | [diff] [blame] | 4139 | if (!mtd->name) |
| 4140 | mtd->name = type->name; |
| 4141 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4142 | chip->chipsize = (uint64_t)type->chipsize << 20; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4143 | |
Boris BREZILLON | a7f5ba4 | 2015-10-01 16:58:27 +0200 | [diff] [blame] | 4144 | if (!type->pagesize) { |
Brian Norris | fc09bbc | 2012-09-24 20:40:50 -0700 | [diff] [blame] | 4145 | /* Decode parameters from extended ID */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4146 | nand_decode_ext_id(chip); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4147 | } else { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4148 | nand_decode_id(chip, type); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4149 | } |
Brian Norris | bf7a01b | 2012-07-13 09:28:24 -0700 | [diff] [blame] | 4150 | /* Get chip options */ |
| 4151 | chip->options |= type->options; |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4152 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4153 | /* |
| 4154 | * Check if chip is not a Samsung device. Do not clear the |
| 4155 | * options for chips which do not have an extended id. |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4156 | */ |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4157 | if (maf_id != NAND_MFR_SAMSUNG && !type->pagesize) |
Florian Fainelli | d1e1f4e | 2010-08-30 18:32:24 +0200 | [diff] [blame] | 4158 | chip->options &= ~NAND_SAMSUNG_LP_OPTIONS; |
| 4159 | ident_done: |
| 4160 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4161 | /* Try to identify manufacturer */ |
David Woodhouse | 9a90986 | 2006-07-15 13:26:18 +0100 | [diff] [blame] | 4162 | for (maf_idx = 0; nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) { |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4163 | if (nand_manuf_ids[maf_idx].id == maf_id) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4164 | break; |
| 4165 | } |
| 4166 | |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 4167 | if (chip->options & NAND_BUSWIDTH_AUTO) { |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4168 | WARN_ON(busw & NAND_BUSWIDTH_16); |
| 4169 | nand_set_defaults(chip); |
Matthieu CASTET | 64b37b2 | 2012-11-06 11:51:44 +0100 | [diff] [blame] | 4170 | } else if (busw != (chip->options & NAND_BUSWIDTH_16)) { |
| 4171 | /* |
| 4172 | * Check, if buswidth is correct. Hardware drivers should set |
| 4173 | * chip correct! |
| 4174 | */ |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4175 | 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] | 4176 | maf_id, dev_id); |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4177 | pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, mtd->name); |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4178 | pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8, |
| 4179 | (chip->options & NAND_BUSWIDTH_16) ? 16 : 8); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4180 | return -EINVAL; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4181 | } |
| 4182 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4183 | nand_decode_bbm_options(chip); |
Brian Norris | 7e74c2d | 2012-09-24 20:40:49 -0700 | [diff] [blame] | 4184 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4185 | /* Calculate the address shift from the page size */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4186 | chip->page_shift = ffs(mtd->writesize) - 1; |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4187 | /* Convert chipsize to number of pages per chip -1 */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4188 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4189 | |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4190 | chip->bbt_erase_shift = chip->phys_erase_shift = |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4191 | ffs(mtd->erasesize) - 1; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 4192 | if (chip->chipsize & 0xffffffff) |
| 4193 | chip->chip_shift = ffs((unsigned)chip->chipsize) - 1; |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4194 | else { |
| 4195 | chip->chip_shift = ffs((unsigned)(chip->chipsize >> 32)); |
| 4196 | chip->chip_shift += 32 - 1; |
| 4197 | } |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4198 | |
Artem Bityutskiy | 26d9be1 | 2011-04-28 20:26:59 +0300 | [diff] [blame] | 4199 | chip->badblockbits = 8; |
Brian Norris | 49c50b9 | 2014-05-06 16:02:19 -0700 | [diff] [blame] | 4200 | chip->erase = single_erase; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4201 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4202 | /* Do not replace user supplied command function! */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4203 | if (mtd->writesize > 512 && chip->cmdfunc == nand_command) |
| 4204 | chip->cmdfunc = nand_command_lp; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4205 | |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4206 | 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] | 4207 | maf_id, dev_id); |
Huang Shijie | ffdac6cd | 2014-02-21 13:39:41 +0800 | [diff] [blame] | 4208 | |
| 4209 | if (chip->onfi_version) |
| 4210 | pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, |
| 4211 | chip->onfi_params.model); |
| 4212 | else if (chip->jedec_version) |
| 4213 | pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, |
| 4214 | chip->jedec_params.model); |
| 4215 | else |
| 4216 | pr_info("%s %s\n", nand_manuf_ids[maf_idx].name, |
| 4217 | type->name); |
| 4218 | |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 4219 | 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] | 4220 | (int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC", |
Rafał Miłecki | 3755a99 | 2014-10-21 00:01:04 +0200 | [diff] [blame] | 4221 | mtd->erasesize >> 10, mtd->writesize, mtd->oobsize); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4222 | return 0; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4223 | } |
| 4224 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4225 | static const char * const nand_ecc_modes[] = { |
| 4226 | [NAND_ECC_NONE] = "none", |
| 4227 | [NAND_ECC_SOFT] = "soft", |
| 4228 | [NAND_ECC_HW] = "hw", |
| 4229 | [NAND_ECC_HW_SYNDROME] = "hw_syndrome", |
| 4230 | [NAND_ECC_HW_OOB_FIRST] = "hw_oob_first", |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4231 | }; |
| 4232 | |
| 4233 | static int of_get_nand_ecc_mode(struct device_node *np) |
| 4234 | { |
| 4235 | const char *pm; |
| 4236 | int err, i; |
| 4237 | |
| 4238 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 4239 | if (err < 0) |
| 4240 | return err; |
| 4241 | |
| 4242 | for (i = 0; i < ARRAY_SIZE(nand_ecc_modes); i++) |
| 4243 | if (!strcasecmp(pm, nand_ecc_modes[i])) |
| 4244 | return i; |
| 4245 | |
Rafał Miłecki | ae211bc | 2016-04-17 22:53:06 +0200 | [diff] [blame] | 4246 | /* |
| 4247 | * For backward compatibility we support few obsoleted values that don't |
| 4248 | * have their mappings into nand_ecc_modes_t anymore (they were merged |
| 4249 | * with other enums). |
| 4250 | */ |
| 4251 | if (!strcasecmp(pm, "soft_bch")) |
| 4252 | return NAND_ECC_SOFT; |
| 4253 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4254 | return -ENODEV; |
| 4255 | } |
| 4256 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4257 | static const char * const nand_ecc_algos[] = { |
| 4258 | [NAND_ECC_HAMMING] = "hamming", |
| 4259 | [NAND_ECC_BCH] = "bch", |
| 4260 | }; |
| 4261 | |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4262 | static int of_get_nand_ecc_algo(struct device_node *np) |
| 4263 | { |
| 4264 | const char *pm; |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4265 | int err, i; |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4266 | |
Rafał Miłecki | ba4f46b | 2016-04-22 13:23:13 +0200 | [diff] [blame] | 4267 | err = of_property_read_string(np, "nand-ecc-algo", &pm); |
| 4268 | if (!err) { |
| 4269 | for (i = NAND_ECC_HAMMING; i < ARRAY_SIZE(nand_ecc_algos); i++) |
| 4270 | if (!strcasecmp(pm, nand_ecc_algos[i])) |
| 4271 | return i; |
| 4272 | return -ENODEV; |
| 4273 | } |
Boris Brezillon | d48f62b | 2016-04-01 14:54:32 +0200 | [diff] [blame] | 4274 | |
| 4275 | /* |
| 4276 | * For backward compatibility we also read "nand-ecc-mode" checking |
| 4277 | * for some obsoleted values that were specifying ECC algorithm. |
| 4278 | */ |
| 4279 | err = of_property_read_string(np, "nand-ecc-mode", &pm); |
| 4280 | if (err < 0) |
| 4281 | return err; |
| 4282 | |
| 4283 | if (!strcasecmp(pm, "soft")) |
| 4284 | return NAND_ECC_HAMMING; |
| 4285 | else if (!strcasecmp(pm, "soft_bch")) |
| 4286 | return NAND_ECC_BCH; |
| 4287 | |
| 4288 | return -ENODEV; |
| 4289 | } |
| 4290 | |
| 4291 | static int of_get_nand_ecc_step_size(struct device_node *np) |
| 4292 | { |
| 4293 | int ret; |
| 4294 | u32 val; |
| 4295 | |
| 4296 | ret = of_property_read_u32(np, "nand-ecc-step-size", &val); |
| 4297 | return ret ? ret : val; |
| 4298 | } |
| 4299 | |
| 4300 | static int of_get_nand_ecc_strength(struct device_node *np) |
| 4301 | { |
| 4302 | int ret; |
| 4303 | u32 val; |
| 4304 | |
| 4305 | ret = of_property_read_u32(np, "nand-ecc-strength", &val); |
| 4306 | return ret ? ret : val; |
| 4307 | } |
| 4308 | |
| 4309 | static int of_get_nand_bus_width(struct device_node *np) |
| 4310 | { |
| 4311 | u32 val; |
| 4312 | |
| 4313 | if (of_property_read_u32(np, "nand-bus-width", &val)) |
| 4314 | return 8; |
| 4315 | |
| 4316 | switch (val) { |
| 4317 | case 8: |
| 4318 | case 16: |
| 4319 | return val; |
| 4320 | default: |
| 4321 | return -EIO; |
| 4322 | } |
| 4323 | } |
| 4324 | |
| 4325 | static bool of_get_nand_on_flash_bbt(struct device_node *np) |
| 4326 | { |
| 4327 | return of_property_read_bool(np, "nand-on-flash-bbt"); |
| 4328 | } |
| 4329 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4330 | static int nand_dt_init(struct nand_chip *chip) |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4331 | { |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4332 | struct device_node *dn = nand_get_flash_node(chip); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4333 | int ecc_mode, ecc_algo, ecc_strength, ecc_step; |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4334 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4335 | if (!dn) |
| 4336 | return 0; |
| 4337 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4338 | if (of_get_nand_bus_width(dn) == 16) |
| 4339 | chip->options |= NAND_BUSWIDTH_16; |
| 4340 | |
| 4341 | if (of_get_nand_on_flash_bbt(dn)) |
| 4342 | chip->bbt_options |= NAND_BBT_USE_FLASH; |
| 4343 | |
| 4344 | ecc_mode = of_get_nand_ecc_mode(dn); |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4345 | ecc_algo = of_get_nand_ecc_algo(dn); |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4346 | ecc_strength = of_get_nand_ecc_strength(dn); |
| 4347 | ecc_step = of_get_nand_ecc_step_size(dn); |
| 4348 | |
| 4349 | if ((ecc_step >= 0 && !(ecc_strength >= 0)) || |
| 4350 | (!(ecc_step >= 0) && ecc_strength >= 0)) { |
| 4351 | pr_err("must set both strength and step size in DT\n"); |
| 4352 | return -EINVAL; |
| 4353 | } |
| 4354 | |
| 4355 | if (ecc_mode >= 0) |
| 4356 | chip->ecc.mode = ecc_mode; |
| 4357 | |
Rafał Miłecki | 7908245 | 2016-03-23 11:19:02 +0100 | [diff] [blame] | 4358 | if (ecc_algo >= 0) |
| 4359 | chip->ecc.algo = ecc_algo; |
| 4360 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4361 | if (ecc_strength >= 0) |
| 4362 | chip->ecc.strength = ecc_strength; |
| 4363 | |
| 4364 | if (ecc_step > 0) |
| 4365 | chip->ecc.size = ecc_step; |
| 4366 | |
Boris Brezillon | ba78ee0 | 2016-06-08 17:04:22 +0200 | [diff] [blame] | 4367 | if (of_property_read_bool(dn, "nand-ecc-maximize")) |
| 4368 | chip->ecc.options |= NAND_ECC_MAXIMIZE; |
| 4369 | |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4370 | return 0; |
| 4371 | } |
| 4372 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4373 | /** |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4374 | * nand_scan_ident - [NAND Interface] Scan for the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4375 | * @mtd: MTD device structure |
| 4376 | * @maxchips: number of chips to scan for |
| 4377 | * @table: alternative NAND ID table |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4378 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4379 | * This is the first phase of the normal nand_scan() function. It reads the |
| 4380 | * flash ID and sets up MTD fields accordingly. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4381 | * |
| 4382 | */ |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4383 | int nand_scan_ident(struct mtd_info *mtd, int maxchips, |
| 4384 | struct nand_flash_dev *table) |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4385 | { |
Cai Zhiyong | bb77082 | 2013-12-25 20:11:15 +0800 | [diff] [blame] | 4386 | int i, nand_maf_id, nand_dev_id; |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4387 | struct nand_chip *chip = mtd_to_nand(mtd); |
Brian Norris | 5844fee | 2015-01-23 00:22:27 -0800 | [diff] [blame] | 4388 | int ret; |
| 4389 | |
Boris BREZILLON | 7194a29a | 2015-12-10 09:00:37 +0100 | [diff] [blame] | 4390 | ret = nand_dt_init(chip); |
| 4391 | if (ret) |
| 4392 | return ret; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4393 | |
Brian Norris | f7a8e38 | 2016-01-05 10:39:45 -0800 | [diff] [blame] | 4394 | if (!mtd->name && mtd->dev.parent) |
| 4395 | mtd->name = dev_name(mtd->dev.parent); |
| 4396 | |
Andrey Smirnov | 76fe334 | 2016-07-21 14:59:20 -0700 | [diff] [blame] | 4397 | if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) { |
| 4398 | /* |
| 4399 | * Default functions assigned for chip_select() and |
| 4400 | * cmdfunc() both expect cmd_ctrl() to be populated, |
| 4401 | * so we need to check that that's the case |
| 4402 | */ |
| 4403 | pr_err("chip.cmd_ctrl() callback is not provided"); |
| 4404 | return -EINVAL; |
| 4405 | } |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4406 | /* Set the default functions */ |
Boris Brezillon | 29a198a | 2016-05-24 20:17:48 +0200 | [diff] [blame] | 4407 | nand_set_defaults(chip); |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4408 | |
| 4409 | /* Read the flash type */ |
Boris Brezillon | 7bb4279 | 2016-05-24 20:55:33 +0200 | [diff] [blame^] | 4410 | ret = nand_detect(chip, table); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4411 | if (ret) { |
Ben Dooks | b1c6e6d | 2009-11-02 18:12:33 +0000 | [diff] [blame] | 4412 | if (!(chip->options & NAND_SCAN_SILENT_NODEV)) |
Brian Norris | d037021 | 2011-07-19 10:06:08 -0700 | [diff] [blame] | 4413 | pr_warn("No NAND device found\n"); |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4414 | chip->select_chip(mtd, -1); |
Masahiro Yamada | 4722c0e | 2016-11-04 17:49:08 +0900 | [diff] [blame] | 4415 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4416 | } |
| 4417 | |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4418 | /* Initialize the ->data_interface field. */ |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 4419 | ret = nand_init_data_interface(chip); |
| 4420 | if (ret) |
| 4421 | return ret; |
| 4422 | |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4423 | /* |
| 4424 | * Setup the data interface correctly on the chip and controller side. |
| 4425 | * This explicit call to nand_setup_data_interface() is only required |
| 4426 | * for the first die, because nand_reset() has been called before |
| 4427 | * ->data_interface and ->default_onfi_timing_mode were set. |
| 4428 | * For the other dies, nand_reset() will automatically switch to the |
| 4429 | * best mode for us. |
| 4430 | */ |
| 4431 | ret = nand_setup_data_interface(chip); |
| 4432 | if (ret) |
| 4433 | return ret; |
| 4434 | |
Boris Brezillon | 7f501f0 | 2016-05-24 19:20:05 +0200 | [diff] [blame] | 4435 | nand_maf_id = chip->id.data[0]; |
| 4436 | nand_dev_id = chip->id.data[1]; |
| 4437 | |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 4438 | chip->select_chip(mtd, -1); |
| 4439 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4440 | /* Check for a chip array */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4441 | for (i = 1; i < maxchips; i++) { |
Karl Beldan | ef89a88 | 2008-09-15 14:37:29 +0200 | [diff] [blame] | 4442 | /* See comment in nand_get_flash_type for reset */ |
Boris Brezillon | 73f907f | 2016-10-24 16:46:20 +0200 | [diff] [blame] | 4443 | nand_reset(chip, i); |
| 4444 | |
| 4445 | chip->select_chip(mtd, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4446 | /* Send the command for reading device ID */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4447 | chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4448 | /* Read manufacturer and device IDs */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4449 | if (nand_maf_id != chip->read_byte(mtd) || |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 4450 | nand_dev_id != chip->read_byte(mtd)) { |
| 4451 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4452 | break; |
Huang Shijie | 0730016 | 2012-11-09 16:23:45 +0800 | [diff] [blame] | 4453 | } |
| 4454 | chip->select_chip(mtd, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4455 | } |
| 4456 | if (i > 1) |
Ezequiel Garcia | 2017164 | 2013-11-25 08:30:31 -0300 | [diff] [blame] | 4457 | pr_info("%d chips detected\n", i); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4459 | /* Store the number of chips and calc total size for mtd */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4460 | chip->numchips = i; |
| 4461 | mtd->size = i * chip->chipsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4462 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4463 | return 0; |
| 4464 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4465 | EXPORT_SYMBOL(nand_scan_ident); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4466 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4467 | static int nand_set_ecc_soft_ops(struct mtd_info *mtd) |
| 4468 | { |
| 4469 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 4470 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 4471 | |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 4472 | if (WARN_ON(ecc->mode != NAND_ECC_SOFT)) |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4473 | return -EINVAL; |
| 4474 | |
| 4475 | switch (ecc->algo) { |
| 4476 | case NAND_ECC_HAMMING: |
| 4477 | ecc->calculate = nand_calculate_ecc; |
| 4478 | ecc->correct = nand_correct_data; |
| 4479 | ecc->read_page = nand_read_page_swecc; |
| 4480 | ecc->read_subpage = nand_read_subpage; |
| 4481 | ecc->write_page = nand_write_page_swecc; |
| 4482 | ecc->read_page_raw = nand_read_page_raw; |
| 4483 | ecc->write_page_raw = nand_write_page_raw; |
| 4484 | ecc->read_oob = nand_read_oob_std; |
| 4485 | ecc->write_oob = nand_write_oob_std; |
| 4486 | if (!ecc->size) |
| 4487 | ecc->size = 256; |
| 4488 | ecc->bytes = 3; |
| 4489 | ecc->strength = 1; |
| 4490 | return 0; |
| 4491 | case NAND_ECC_BCH: |
| 4492 | if (!mtd_nand_has_bch()) { |
| 4493 | WARN(1, "CONFIG_MTD_NAND_ECC_BCH not enabled\n"); |
| 4494 | return -EINVAL; |
| 4495 | } |
| 4496 | ecc->calculate = nand_bch_calculate_ecc; |
| 4497 | ecc->correct = nand_bch_correct_data; |
| 4498 | ecc->read_page = nand_read_page_swecc; |
| 4499 | ecc->read_subpage = nand_read_subpage; |
| 4500 | ecc->write_page = nand_write_page_swecc; |
| 4501 | ecc->read_page_raw = nand_read_page_raw; |
| 4502 | ecc->write_page_raw = nand_write_page_raw; |
| 4503 | ecc->read_oob = nand_read_oob_std; |
| 4504 | ecc->write_oob = nand_write_oob_std; |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 4505 | |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4506 | /* |
| 4507 | * Board driver should supply ecc.size and ecc.strength |
| 4508 | * values to select how many bits are correctable. |
| 4509 | * Otherwise, default to 4 bits for large page devices. |
| 4510 | */ |
| 4511 | if (!ecc->size && (mtd->oobsize >= 64)) { |
| 4512 | ecc->size = 512; |
| 4513 | ecc->strength = 4; |
| 4514 | } |
| 4515 | |
| 4516 | /* |
| 4517 | * if no ecc placement scheme was provided pickup the default |
| 4518 | * large page one. |
| 4519 | */ |
| 4520 | if (!mtd->ooblayout) { |
| 4521 | /* handle large page devices only */ |
| 4522 | if (mtd->oobsize < 64) { |
| 4523 | WARN(1, "OOB layout is required when using software BCH on small pages\n"); |
| 4524 | return -EINVAL; |
| 4525 | } |
| 4526 | |
| 4527 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops); |
Boris Brezillon | 8bbba48 | 2016-06-08 17:04:23 +0200 | [diff] [blame] | 4528 | |
| 4529 | } |
| 4530 | |
| 4531 | /* |
| 4532 | * We can only maximize ECC config when the default layout is |
| 4533 | * used, otherwise we don't know how many bytes can really be |
| 4534 | * used. |
| 4535 | */ |
| 4536 | if (mtd->ooblayout == &nand_ooblayout_lp_ops && |
| 4537 | ecc->options & NAND_ECC_MAXIMIZE) { |
| 4538 | int steps, bytes; |
| 4539 | |
| 4540 | /* Always prefer 1k blocks over 512bytes ones */ |
| 4541 | ecc->size = 1024; |
| 4542 | steps = mtd->writesize / ecc->size; |
| 4543 | |
| 4544 | /* Reserve 2 bytes for the BBM */ |
| 4545 | bytes = (mtd->oobsize - 2) / steps; |
| 4546 | ecc->strength = bytes * 8 / fls(8 * ecc->size); |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4547 | } |
| 4548 | |
| 4549 | /* See nand_bch_init() for details. */ |
| 4550 | ecc->bytes = 0; |
| 4551 | ecc->priv = nand_bch_init(mtd); |
| 4552 | if (!ecc->priv) { |
| 4553 | WARN(1, "BCH ECC initialization failed!\n"); |
| 4554 | return -EINVAL; |
| 4555 | } |
| 4556 | return 0; |
| 4557 | default: |
| 4558 | WARN(1, "Unsupported ECC algorithm!\n"); |
| 4559 | return -EINVAL; |
| 4560 | } |
| 4561 | } |
| 4562 | |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 4563 | /* |
| 4564 | * Check if the chip configuration meet the datasheet requirements. |
| 4565 | |
| 4566 | * If our configuration corrects A bits per B bytes and the minimum |
| 4567 | * required correction level is X bits per Y bytes, then we must ensure |
| 4568 | * both of the following are true: |
| 4569 | * |
| 4570 | * (1) A / B >= X / Y |
| 4571 | * (2) A >= X |
| 4572 | * |
| 4573 | * Requirement (1) ensures we can correct for the required bitflip density. |
| 4574 | * Requirement (2) ensures we can correct even when all bitflips are clumped |
| 4575 | * in the same sector. |
| 4576 | */ |
| 4577 | static bool nand_ecc_strength_good(struct mtd_info *mtd) |
| 4578 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4579 | struct nand_chip *chip = mtd_to_nand(mtd); |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 4580 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 4581 | int corr, ds_corr; |
| 4582 | |
| 4583 | if (ecc->size == 0 || chip->ecc_step_ds == 0) |
| 4584 | /* Not enough information */ |
| 4585 | return true; |
| 4586 | |
| 4587 | /* |
| 4588 | * We get the number of corrected bits per page to compare |
| 4589 | * the correction density. |
| 4590 | */ |
| 4591 | corr = (mtd->writesize * ecc->strength) / ecc->size; |
| 4592 | ds_corr = (mtd->writesize * chip->ecc_strength_ds) / chip->ecc_step_ds; |
| 4593 | |
| 4594 | return corr >= ds_corr && ecc->strength >= chip->ecc_strength_ds; |
| 4595 | } |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4596 | |
Marc Gonzalez | 3371d66 | 2016-11-15 10:56:20 +0100 | [diff] [blame] | 4597 | static bool invalid_ecc_page_accessors(struct nand_chip *chip) |
| 4598 | { |
| 4599 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 4600 | |
| 4601 | if (nand_standard_page_accessors(ecc)) |
| 4602 | return false; |
| 4603 | |
| 4604 | /* |
| 4605 | * NAND_ECC_CUSTOM_PAGE_ACCESS flag is set, make sure the NAND |
| 4606 | * controller driver implements all the page accessors because |
| 4607 | * default helpers are not suitable when the core does not |
| 4608 | * send the READ0/PAGEPROG commands. |
| 4609 | */ |
| 4610 | return (!ecc->read_page || !ecc->write_page || |
| 4611 | !ecc->read_page_raw || !ecc->write_page_raw || |
| 4612 | (NAND_HAS_SUBPAGE_READ(chip) && !ecc->read_subpage) || |
| 4613 | (NAND_HAS_SUBPAGE_WRITE(chip) && !ecc->write_subpage && |
| 4614 | ecc->hwctl && ecc->calculate)); |
| 4615 | } |
| 4616 | |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4617 | /** |
| 4618 | * nand_scan_tail - [NAND Interface] Scan for the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4619 | * @mtd: MTD device structure |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4620 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4621 | * This is the second phase of the normal nand_scan() function. It fills out |
| 4622 | * all the uninitialized function pointers with the defaults and scans for a |
| 4623 | * bad block table if appropriate. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4624 | */ |
| 4625 | int nand_scan_tail(struct mtd_info *mtd) |
| 4626 | { |
Boris BREZILLON | 862eba5 | 2015-12-01 12:03:03 +0100 | [diff] [blame] | 4627 | struct nand_chip *chip = mtd_to_nand(mtd); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4628 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
Huang Shijie | f02ea4e | 2014-01-13 14:27:12 +0800 | [diff] [blame] | 4629 | struct nand_buffers *nbuf; |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4630 | int ret; |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4631 | |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 4632 | /* 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] | 4633 | if (WARN_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) && |
| 4634 | !(chip->bbt_options & NAND_BBT_USE_FLASH))) |
| 4635 | return -EINVAL; |
Brian Norris | e2414f4 | 2012-02-06 13:44:00 -0800 | [diff] [blame] | 4636 | |
Marc Gonzalez | 3371d66 | 2016-11-15 10:56:20 +0100 | [diff] [blame] | 4637 | if (invalid_ecc_page_accessors(chip)) { |
| 4638 | pr_err("Invalid ECC page accessors setup\n"); |
| 4639 | return -EINVAL; |
| 4640 | } |
| 4641 | |
Huang Shijie | f02ea4e | 2014-01-13 14:27:12 +0800 | [diff] [blame] | 4642 | if (!(chip->options & NAND_OWN_BUFFERS)) { |
| 4643 | nbuf = kzalloc(sizeof(*nbuf) + mtd->writesize |
| 4644 | + mtd->oobsize * 3, GFP_KERNEL); |
| 4645 | if (!nbuf) |
| 4646 | return -ENOMEM; |
| 4647 | nbuf->ecccalc = (uint8_t *)(nbuf + 1); |
| 4648 | nbuf->ecccode = nbuf->ecccalc + mtd->oobsize; |
| 4649 | nbuf->databuf = nbuf->ecccode + mtd->oobsize; |
| 4650 | |
| 4651 | chip->buffers = nbuf; |
| 4652 | } else { |
| 4653 | if (!chip->buffers) |
| 4654 | return -ENOMEM; |
| 4655 | } |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 4656 | |
David Woodhouse | 7dcdcbef | 2006-10-21 17:09:53 +0100 | [diff] [blame] | 4657 | /* Set the internal oob buffer location, just after the page data */ |
David Woodhouse | 784f4d5 | 2006-10-22 01:47:45 +0100 | [diff] [blame] | 4658 | chip->oob_poi = chip->buffers->databuf + mtd->writesize; |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4659 | |
| 4660 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4661 | * If no default placement scheme is given, select an appropriate one. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4662 | */ |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4663 | if (!mtd->ooblayout && |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 4664 | !(ecc->mode == NAND_ECC_SOFT && ecc->algo == NAND_ECC_BCH)) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4665 | switch (mtd->oobsize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4666 | case 8: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4667 | case 16: |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 4668 | mtd_set_ooblayout(mtd, &nand_ooblayout_sp_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4669 | break; |
| 4670 | case 64: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 4671 | case 128: |
Boris Brezillon | 41b207a | 2016-02-03 19:06:15 +0100 | [diff] [blame] | 4672 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops); |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 4673 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4674 | default: |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4675 | WARN(1, "No oob scheme defined for oobsize %d\n", |
| 4676 | mtd->oobsize); |
| 4677 | ret = -EINVAL; |
| 4678 | goto err_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4679 | } |
| 4680 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4681 | |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 4682 | if (!chip->write_page) |
| 4683 | chip->write_page = nand_write_page; |
| 4684 | |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4685 | /* |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4686 | * Check ECC mode, default to software if 3byte/512byte hardware ECC is |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4687 | * selected and we have 256 byte pagesize fallback to software ECC |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4688 | */ |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 4689 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4690 | switch (ecc->mode) { |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 4691 | case NAND_ECC_HW_OOB_FIRST: |
| 4692 | /* Similar to NAND_ECC_HW, but a separate read_page handle */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4693 | if (!ecc->calculate || !ecc->correct || !ecc->hwctl) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4694 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 4695 | ret = -EINVAL; |
| 4696 | goto err_free; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 4697 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4698 | if (!ecc->read_page) |
| 4699 | ecc->read_page = nand_read_page_hwecc_oob_first; |
Sneha Narnakaje | 6e0cb13 | 2009-09-18 12:51:47 -0700 | [diff] [blame] | 4700 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 4701 | case NAND_ECC_HW: |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4702 | /* Use standard hwecc read page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4703 | if (!ecc->read_page) |
| 4704 | ecc->read_page = nand_read_page_hwecc; |
| 4705 | if (!ecc->write_page) |
| 4706 | ecc->write_page = nand_write_page_hwecc; |
| 4707 | if (!ecc->read_page_raw) |
| 4708 | ecc->read_page_raw = nand_read_page_raw; |
| 4709 | if (!ecc->write_page_raw) |
| 4710 | ecc->write_page_raw = nand_write_page_raw; |
| 4711 | if (!ecc->read_oob) |
| 4712 | ecc->read_oob = nand_read_oob_std; |
| 4713 | if (!ecc->write_oob) |
| 4714 | ecc->write_oob = nand_write_oob_std; |
| 4715 | if (!ecc->read_subpage) |
| 4716 | ecc->read_subpage = nand_read_subpage; |
Helmut Schaa | 44991b3 | 2014-04-09 11:13:24 +0200 | [diff] [blame] | 4717 | if (!ecc->write_subpage && ecc->hwctl && ecc->calculate) |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4718 | ecc->write_subpage = nand_write_subpage_hwecc; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 4719 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 4720 | case NAND_ECC_HW_SYNDROME: |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4721 | if ((!ecc->calculate || !ecc->correct || !ecc->hwctl) && |
| 4722 | (!ecc->read_page || |
| 4723 | ecc->read_page == nand_read_page_hwecc || |
| 4724 | !ecc->write_page || |
| 4725 | ecc->write_page == nand_write_page_hwecc)) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4726 | WARN(1, "No ECC functions supplied; hardware ECC not possible\n"); |
| 4727 | ret = -EINVAL; |
| 4728 | goto err_free; |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 4729 | } |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4730 | /* Use standard syndrome read/write page function? */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4731 | if (!ecc->read_page) |
| 4732 | ecc->read_page = nand_read_page_syndrome; |
| 4733 | if (!ecc->write_page) |
| 4734 | ecc->write_page = nand_write_page_syndrome; |
| 4735 | if (!ecc->read_page_raw) |
| 4736 | ecc->read_page_raw = nand_read_page_raw_syndrome; |
| 4737 | if (!ecc->write_page_raw) |
| 4738 | ecc->write_page_raw = nand_write_page_raw_syndrome; |
| 4739 | if (!ecc->read_oob) |
| 4740 | ecc->read_oob = nand_read_oob_syndrome; |
| 4741 | if (!ecc->write_oob) |
| 4742 | ecc->write_oob = nand_write_oob_syndrome; |
Thomas Gleixner | f5bbdac | 2006-05-25 10:07:16 +0200 | [diff] [blame] | 4743 | |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4744 | if (mtd->writesize >= ecc->size) { |
| 4745 | if (!ecc->strength) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4746 | WARN(1, "Driver must set ecc.strength when using hardware ECC\n"); |
| 4747 | ret = -EINVAL; |
| 4748 | goto err_free; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 4749 | } |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 4750 | break; |
Mike Dunn | e2788c9 | 2012-04-25 12:06:10 -0700 | [diff] [blame] | 4751 | } |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 4752 | pr_warn("%d byte HW ECC not possible on %d byte page size, fallback to SW ECC\n", |
| 4753 | ecc->size, mtd->writesize); |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4754 | ecc->mode = NAND_ECC_SOFT; |
Rafał Miłecki | e9d4fae | 2016-04-17 22:53:02 +0200 | [diff] [blame] | 4755 | ecc->algo = NAND_ECC_HAMMING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4756 | |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 4757 | case NAND_ECC_SOFT: |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4758 | ret = nand_set_ecc_soft_ops(mtd); |
| 4759 | if (ret) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4760 | ret = -EINVAL; |
| 4761 | goto err_free; |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 4762 | } |
| 4763 | break; |
| 4764 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4765 | case NAND_ECC_NONE: |
Rafał Miłecki | 2ac63d9 | 2014-08-19 13:55:34 +0200 | [diff] [blame] | 4766 | 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] | 4767 | ecc->read_page = nand_read_page_raw; |
| 4768 | ecc->write_page = nand_write_page_raw; |
| 4769 | ecc->read_oob = nand_read_oob_std; |
| 4770 | ecc->read_page_raw = nand_read_page_raw; |
| 4771 | ecc->write_page_raw = nand_write_page_raw; |
| 4772 | ecc->write_oob = nand_write_oob_std; |
| 4773 | ecc->size = mtd->writesize; |
| 4774 | ecc->bytes = 0; |
| 4775 | ecc->strength = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4776 | break; |
David Woodhouse | 956e944 | 2006-09-25 17:12:39 +0100 | [diff] [blame] | 4777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4778 | default: |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4779 | WARN(1, "Invalid NAND_ECC_MODE %d\n", ecc->mode); |
| 4780 | ret = -EINVAL; |
| 4781 | goto err_free; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4782 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4783 | |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 4784 | /* For many systems, the standard OOB write also works for raw */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4785 | if (!ecc->read_oob_raw) |
| 4786 | ecc->read_oob_raw = ecc->read_oob; |
| 4787 | if (!ecc->write_oob_raw) |
| 4788 | ecc->write_oob_raw = ecc->write_oob; |
Brian Norris | 9ce244b | 2011-08-30 18:45:37 -0700 | [diff] [blame] | 4789 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4790 | /* propagate ecc info to mtd_info */ |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4791 | mtd->ecc_strength = ecc->strength; |
| 4792 | mtd->ecc_step_size = ecc->size; |
Ezequiel Garcia | 67a9ad9 | 2014-05-14 14:58:06 -0300 | [diff] [blame] | 4793 | |
Thomas Gleixner | 5bd34c0 | 2006-05-27 22:16:10 +0200 | [diff] [blame] | 4794 | /* |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4795 | * 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] | 4796 | * mode. |
Thomas Gleixner | 7aa65bf | 2006-05-23 11:54:38 +0200 | [diff] [blame] | 4797 | */ |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4798 | ecc->steps = mtd->writesize / ecc->size; |
| 4799 | if (ecc->steps * ecc->size != mtd->writesize) { |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4800 | WARN(1, "Invalid ECC parameters\n"); |
| 4801 | ret = -EINVAL; |
| 4802 | goto err_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4803 | } |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4804 | ecc->total = ecc->steps * ecc->bytes; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 4805 | |
Boris Brezillon | 846031d | 2016-02-03 20:11:00 +0100 | [diff] [blame] | 4806 | /* |
| 4807 | * The number of bytes available for a client to place data into |
| 4808 | * the out of band area. |
| 4809 | */ |
| 4810 | ret = mtd_ooblayout_count_freebytes(mtd); |
| 4811 | if (ret < 0) |
| 4812 | ret = 0; |
| 4813 | |
| 4814 | mtd->oobavail = ret; |
| 4815 | |
| 4816 | /* ECC sanity check: warn if it's too weak */ |
| 4817 | if (!nand_ecc_strength_good(mtd)) |
| 4818 | pr_warn("WARNING: %s: the ECC used on your system is too weak compared to the one required by the NAND chip\n", |
| 4819 | mtd->name); |
| 4820 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4821 | /* Allow subpage writes up to ecc.steps. Not possible for MLC flash */ |
Huang Shijie | 1d0ed69 | 2013-09-25 14:58:10 +0800 | [diff] [blame] | 4822 | if (!(chip->options & NAND_NO_SUBPAGE_WRITE) && nand_is_slc(chip)) { |
Huang Shijie | 97de79e0 | 2013-10-18 14:20:53 +0800 | [diff] [blame] | 4823 | switch (ecc->steps) { |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4824 | case 2: |
| 4825 | mtd->subpage_sft = 1; |
| 4826 | break; |
| 4827 | case 4: |
| 4828 | case 8: |
Thomas Gleixner | 81ec536 | 2007-12-12 17:27:03 +0100 | [diff] [blame] | 4829 | case 16: |
Thomas Gleixner | 29072b9 | 2006-09-28 15:38:36 +0200 | [diff] [blame] | 4830 | mtd->subpage_sft = 2; |
| 4831 | break; |
| 4832 | } |
| 4833 | } |
| 4834 | chip->subpagesize = mtd->writesize >> mtd->subpage_sft; |
| 4835 | |
Thomas Gleixner | 04bbd0e | 2006-05-25 09:45:29 +0200 | [diff] [blame] | 4836 | /* Initialize state */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4837 | chip->state = FL_READY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4839 | /* Invalidate the pagebuffer reference */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4840 | chip->pagebuf = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4841 | |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 4842 | /* Large page NAND with SOFT_ECC should support subpage reads */ |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 4843 | switch (ecc->mode) { |
| 4844 | case NAND_ECC_SOFT: |
Ron Lee | 4007e2d | 2014-04-25 15:01:35 +0930 | [diff] [blame] | 4845 | if (chip->page_shift > 9) |
| 4846 | chip->options |= NAND_SUBPAGE_READ; |
| 4847 | break; |
| 4848 | |
| 4849 | default: |
| 4850 | break; |
| 4851 | } |
Jeff Westfahl | a5ff4f1 | 2012-08-13 16:35:30 -0500 | [diff] [blame] | 4852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4853 | /* Fill in remaining MTD driver data */ |
Huang Shijie | 963d1c2 | 2013-09-25 14:58:21 +0800 | [diff] [blame] | 4854 | mtd->type = nand_is_slc(chip) ? MTD_NANDFLASH : MTD_MLCNANDFLASH; |
Maxim Levitsky | 93edbad | 2010-02-22 20:39:40 +0200 | [diff] [blame] | 4855 | mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM : |
| 4856 | MTD_CAP_NANDFLASH; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 4857 | mtd->_erase = nand_erase; |
| 4858 | mtd->_point = NULL; |
| 4859 | mtd->_unpoint = NULL; |
| 4860 | mtd->_read = nand_read; |
| 4861 | mtd->_write = nand_write; |
| 4862 | mtd->_panic_write = panic_nand_write; |
| 4863 | mtd->_read_oob = nand_read_oob; |
| 4864 | mtd->_write_oob = nand_write_oob; |
| 4865 | mtd->_sync = nand_sync; |
| 4866 | mtd->_lock = NULL; |
| 4867 | mtd->_unlock = NULL; |
| 4868 | mtd->_suspend = nand_suspend; |
| 4869 | mtd->_resume = nand_resume; |
Scott Branden | 72ea403 | 2014-11-20 11:18:05 -0800 | [diff] [blame] | 4870 | mtd->_reboot = nand_shutdown; |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 4871 | mtd->_block_isreserved = nand_block_isreserved; |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 4872 | mtd->_block_isbad = nand_block_isbad; |
| 4873 | mtd->_block_markbad = nand_block_markbad; |
Zach Brown | 5671842 | 2017-01-10 13:30:20 -0600 | [diff] [blame] | 4874 | mtd->_max_bad_blocks = nand_max_bad_blocks; |
Anatolij Gustschin | cbcab65 | 2010-12-16 23:42:16 +0100 | [diff] [blame] | 4875 | mtd->writebufsize = mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4876 | |
Shmulik Ladkani | ea3b2ea | 2012-06-08 18:29:06 +0300 | [diff] [blame] | 4877 | /* |
| 4878 | * Initialize bitflip_threshold to its default prior scan_bbt() call. |
| 4879 | * scan_bbt() might invoke mtd_read(), thus bitflip_threshold must be |
| 4880 | * properly set. |
| 4881 | */ |
| 4882 | if (!mtd->bitflip_threshold) |
Brian Norris | 240181f | 2015-01-12 12:51:29 -0800 | [diff] [blame] | 4883 | mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4884 | |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 4885 | /* Check, if we should skip the bad block table scan */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4886 | if (chip->options & NAND_SKIP_BBTSCAN) |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 4887 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4888 | |
| 4889 | /* Build bad block table */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4890 | return chip->scan_bbt(mtd); |
Ezequiel García | 11eaf6d | 2016-04-01 18:29:24 -0300 | [diff] [blame] | 4891 | err_free: |
| 4892 | if (!(chip->options & NAND_OWN_BUFFERS)) |
| 4893 | kfree(chip->buffers); |
| 4894 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4895 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4896 | EXPORT_SYMBOL(nand_scan_tail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4897 | |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4898 | /* |
| 4899 | * is_module_text_address() isn't exported, and it's mostly a pointless |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4900 | * test if this is a module _anyway_ -- they'd have to try _really_ hard |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4901 | * to call us from in-kernel code if the core NAND support is modular. |
| 4902 | */ |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4903 | #ifdef MODULE |
| 4904 | #define caller_is_module() (1) |
| 4905 | #else |
| 4906 | #define caller_is_module() \ |
Rusty Russell | a6e6abd | 2009-03-31 13:05:31 -0600 | [diff] [blame] | 4907 | is_module_text_address((unsigned long)__builtin_return_address(0)) |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4908 | #endif |
| 4909 | |
| 4910 | /** |
| 4911 | * nand_scan - [NAND Interface] Scan for the NAND device |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4912 | * @mtd: MTD device structure |
| 4913 | * @maxchips: number of chips to scan for |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4914 | * |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4915 | * This fills out all the uninitialized function pointers with the defaults. |
| 4916 | * 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] | 4917 | * appropriate values. |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4918 | */ |
| 4919 | int nand_scan(struct mtd_info *mtd, int maxchips) |
| 4920 | { |
| 4921 | int ret; |
| 4922 | |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 4923 | ret = nand_scan_ident(mtd, maxchips, NULL); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4924 | if (!ret) |
| 4925 | ret = nand_scan_tail(mtd); |
| 4926 | return ret; |
| 4927 | } |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4928 | EXPORT_SYMBOL(nand_scan); |
David Woodhouse | 3b85c32 | 2006-09-25 17:06:53 +0100 | [diff] [blame] | 4929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4930 | /** |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 4931 | * nand_cleanup - [NAND Interface] Free resources held by the NAND device |
| 4932 | * @chip: NAND chip object |
Brian Norris | 8b6e50c | 2011-05-25 14:59:01 -0700 | [diff] [blame] | 4933 | */ |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 4934 | void nand_cleanup(struct nand_chip *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4935 | { |
Rafał Miłecki | e4225ae | 2016-04-17 22:53:07 +0200 | [diff] [blame] | 4936 | if (chip->ecc.mode == NAND_ECC_SOFT && |
Rafał Miłecki | 06f384c | 2016-04-17 22:53:05 +0200 | [diff] [blame] | 4937 | chip->ecc.algo == NAND_ECC_BCH) |
Ivan Djelic | 193bd40 | 2011-03-11 11:05:33 +0100 | [diff] [blame] | 4938 | nand_bch_free((struct nand_bch_control *)chip->ecc.priv); |
| 4939 | |
Boris Brezillon | d8e725d | 2016-09-15 10:32:50 +0200 | [diff] [blame] | 4940 | nand_release_data_interface(chip); |
| 4941 | |
Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 4942 | /* Free bad block table memory */ |
Thomas Gleixner | ace4dfe | 2006-05-24 12:07:37 +0200 | [diff] [blame] | 4943 | kfree(chip->bbt); |
David Woodhouse | 4bf63fc | 2006-09-25 17:08:04 +0100 | [diff] [blame] | 4944 | if (!(chip->options & NAND_OWN_BUFFERS)) |
| 4945 | kfree(chip->buffers); |
Brian Norris | 58373ff | 2010-07-15 12:15:44 -0700 | [diff] [blame] | 4946 | |
| 4947 | /* Free bad block descriptor memory */ |
| 4948 | if (chip->badblock_pattern && chip->badblock_pattern->options |
| 4949 | & NAND_BBT_DYNAMICSTRUCT) |
| 4950 | kfree(chip->badblock_pattern); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4951 | } |
Richard Weinberger | d44154f | 2016-09-21 11:44:41 +0200 | [diff] [blame] | 4952 | EXPORT_SYMBOL_GPL(nand_cleanup); |
| 4953 | |
| 4954 | /** |
| 4955 | * nand_release - [NAND Interface] Unregister the MTD device and free resources |
| 4956 | * held by the NAND device |
| 4957 | * @mtd: MTD device structure |
| 4958 | */ |
| 4959 | void nand_release(struct mtd_info *mtd) |
| 4960 | { |
| 4961 | mtd_device_unregister(mtd); |
| 4962 | nand_cleanup(mtd_to_nand(mtd)); |
| 4963 | } |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4964 | EXPORT_SYMBOL_GPL(nand_release); |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 4965 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4966 | MODULE_LICENSE("GPL"); |
Florian Fainelli | 7351d3a | 2010-09-07 13:23:45 +0200 | [diff] [blame] | 4967 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>"); |
| 4968 | MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>"); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 4969 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |