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