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