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