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