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