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