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 |
| 10 | * http://www.linux-mtd.infradead.org/tech/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) |
| 13 | * 2002 Thomas Gleixner (tglx@linutronix.de) |
| 14 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 15 | * 02-08-2004 tglx: support for strange chips, which cannot auto increment |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | * pages on read / read_oob |
| 17 | * |
| 18 | * 03-17-2004 tglx: Check ready before auto increment check. Simon Bayes |
| 19 | * pointed this out, as he marked an auto increment capable chip |
| 20 | * as NOAUTOINCR in the board driver. |
| 21 | * Make reads over block boundaries work too |
| 22 | * |
| 23 | * 04-14-2004 tglx: first working version for 2k page size chips |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 24 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | * 05-19-2004 tglx: Basic support for Renesas AG-AND chips |
| 26 | * |
| 27 | * 09-24-2004 tglx: add support for hardware controllers (e.g. ECC) shared |
| 28 | * among multiple independend devices. Suggestions and initial patch |
| 29 | * from Ben Dooks <ben-mtd@fluff.org> |
| 30 | * |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 31 | * 12-05-2004 dmarlin: add workaround for Renesas AG-AND chips "disturb" issue. |
| 32 | * Basically, any block not rewritten may lose data when surrounding blocks |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 33 | * are rewritten many times. JFFS2 ensures this doesn't happen for blocks |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 34 | * it uses, but the Bad Block Table(s) may not be rewritten. To ensure they |
| 35 | * do not lose data, force them to be rewritten when some of the surrounding |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 36 | * blocks are erased. Rather than tracking a specific nearby block (which |
| 37 | * could itself go bad), use a page address 'mask' to select several blocks |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 38 | * in the same area, and rewrite the BBT when any of them are erased. |
| 39 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 40 | * 01-03-2005 dmarlin: added support for the device recovery command sequence for Renesas |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 41 | * AG-AND chips. If there was a sudden loss of power during an erase operation, |
| 42 | * a "device recovery" operation must be performed when power is restored |
| 43 | * to ensure correct operation. |
| 44 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 45 | * 01-20-2005 dmarlin: added support for optional hardware specific callback routine to |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 46 | * perform extra error status checks on erase and write failures. This required |
| 47 | * adding a wrapper function for nand_read_ecc. |
| 48 | * |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 49 | * 08-20-2005 vwool: suspend/resume added |
| 50 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | * Credits: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 52 | * David Woodhouse for adding multichip support |
| 53 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | * Aleph One Ltd. and Toby Churchill Ltd. for supporting the |
| 55 | * rework for 2K page size chips |
| 56 | * |
| 57 | * TODO: |
| 58 | * Enable cached programming for 2k page size chips |
| 59 | * Check, if mtd->ecctype should be set to MTD_ECC_HW |
| 60 | * if we have HW ecc support. |
| 61 | * The AG-AND chips have nice features for speed improvement, |
| 62 | * which are not supported yet. Read / program 4 pages in one go. |
| 63 | * |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 64 | * $Id: nand_base.c,v 1.150 2005/09/15 13:58:48 vwool Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | * |
| 66 | * This program is free software; you can redistribute it and/or modify |
| 67 | * it under the terms of the GNU General Public License version 2 as |
| 68 | * published by the Free Software Foundation. |
| 69 | * |
| 70 | */ |
| 71 | |
David Woodhouse | 552d920 | 2006-05-14 01:20:46 +0100 | [diff] [blame] | 72 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | #include <linux/delay.h> |
| 74 | #include <linux/errno.h> |
| 75 | #include <linux/sched.h> |
| 76 | #include <linux/slab.h> |
| 77 | #include <linux/types.h> |
| 78 | #include <linux/mtd/mtd.h> |
| 79 | #include <linux/mtd/nand.h> |
| 80 | #include <linux/mtd/nand_ecc.h> |
| 81 | #include <linux/mtd/compatmac.h> |
| 82 | #include <linux/interrupt.h> |
| 83 | #include <linux/bitops.h> |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 84 | #include <linux/leds.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | #include <asm/io.h> |
| 86 | |
| 87 | #ifdef CONFIG_MTD_PARTITIONS |
| 88 | #include <linux/mtd/partitions.h> |
| 89 | #endif |
| 90 | |
| 91 | /* Define default oob placement schemes for large and small page devices */ |
| 92 | static struct nand_oobinfo nand_oob_8 = { |
| 93 | .useecc = MTD_NANDECC_AUTOPLACE, |
| 94 | .eccbytes = 3, |
| 95 | .eccpos = {0, 1, 2}, |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 96 | .oobfree = {{3, 2}, {6, 2}} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | static struct nand_oobinfo nand_oob_16 = { |
| 100 | .useecc = MTD_NANDECC_AUTOPLACE, |
| 101 | .eccbytes = 6, |
| 102 | .eccpos = {0, 1, 2, 3, 6, 7}, |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 103 | .oobfree = {{8, 8}} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | static struct nand_oobinfo nand_oob_64 = { |
| 107 | .useecc = MTD_NANDECC_AUTOPLACE, |
| 108 | .eccbytes = 24, |
| 109 | .eccpos = { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 110 | 40, 41, 42, 43, 44, 45, 46, 47, |
| 111 | 48, 49, 50, 51, 52, 53, 54, 55, |
| 112 | 56, 57, 58, 59, 60, 61, 62, 63}, |
| 113 | .oobfree = {{2, 38}} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | /* This is used for padding purposes in nand_write_oob */ |
| 117 | static u_char ffchars[] = { |
| 118 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 119 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 120 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 121 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 122 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 123 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 124 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 125 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 126 | }; |
| 127 | |
| 128 | /* |
| 129 | * NAND low-level MTD interface functions |
| 130 | */ |
| 131 | static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len); |
| 132 | static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len); |
| 133 | static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len); |
| 134 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 135 | static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf); |
| 136 | static int nand_read_ecc(struct mtd_info *mtd, loff_t from, size_t len, |
| 137 | size_t *retlen, u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel); |
| 138 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf); |
| 139 | static int nand_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf); |
| 140 | static int nand_write_ecc(struct mtd_info *mtd, loff_t to, size_t len, |
| 141 | size_t *retlen, const u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel); |
| 142 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf); |
| 143 | static int nand_writev(struct mtd_info *mtd, const struct kvec *vecs, unsigned long count, loff_t to, size_t *retlen); |
| 144 | static int nand_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs, |
| 145 | unsigned long count, loff_t to, size_t *retlen, u_char *eccbuf, |
| 146 | struct nand_oobinfo *oobsel); |
| 147 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr); |
| 148 | static void nand_sync(struct mtd_info *mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | /* Some internal functions */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 151 | static int nand_write_page(struct mtd_info *mtd, struct nand_chip *this, int page, u_char * oob_buf, |
| 152 | struct nand_oobinfo *oobsel, int mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | #ifdef CONFIG_MTD_NAND_VERIFY_WRITE |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 154 | static int nand_verify_pages(struct mtd_info *mtd, struct nand_chip *this, int page, int numpages, |
| 155 | u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | #else |
| 157 | #define nand_verify_pages(...) (0) |
| 158 | #endif |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 159 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 160 | static int nand_get_device(struct nand_chip *this, struct mtd_info *mtd, int new_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
| 162 | /** |
| 163 | * nand_release_device - [GENERIC] release chip |
| 164 | * @mtd: MTD device structure |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 165 | * |
| 166 | * Deselect, release chip lock and wake up anyone waiting on the device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 168 | static void nand_release_device(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | { |
| 170 | struct nand_chip *this = mtd->priv; |
| 171 | |
| 172 | /* De-select the NAND device */ |
| 173 | this->select_chip(mtd, -1); |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | if (this->controller) { |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 176 | /* Release the controller and the chip */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | spin_lock(&this->controller->lock); |
| 178 | this->controller->active = NULL; |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 179 | this->state = FL_READY; |
| 180 | wake_up(&this->controller->wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | spin_unlock(&this->controller->lock); |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 182 | } else { |
| 183 | /* Release the chip */ |
| 184 | spin_lock(&this->chip_lock); |
| 185 | this->state = FL_READY; |
| 186 | wake_up(&this->wq); |
| 187 | spin_unlock(&this->chip_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
| 192 | * nand_read_byte - [DEFAULT] read one byte from the chip |
| 193 | * @mtd: MTD device structure |
| 194 | * |
| 195 | * Default read function for 8bit buswith |
| 196 | */ |
| 197 | static u_char nand_read_byte(struct mtd_info *mtd) |
| 198 | { |
| 199 | struct nand_chip *this = mtd->priv; |
| 200 | return readb(this->IO_ADDR_R); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * nand_write_byte - [DEFAULT] write one byte to the chip |
| 205 | * @mtd: MTD device structure |
| 206 | * @byte: pointer to data byte to write |
| 207 | * |
| 208 | * Default write function for 8it buswith |
| 209 | */ |
| 210 | static void nand_write_byte(struct mtd_info *mtd, u_char byte) |
| 211 | { |
| 212 | struct nand_chip *this = mtd->priv; |
| 213 | writeb(byte, this->IO_ADDR_W); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * nand_read_byte16 - [DEFAULT] read one byte endianess aware from the chip |
| 218 | * @mtd: MTD device structure |
| 219 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 220 | * Default read function for 16bit buswith with |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | * endianess conversion |
| 222 | */ |
| 223 | static u_char nand_read_byte16(struct mtd_info *mtd) |
| 224 | { |
| 225 | struct nand_chip *this = mtd->priv; |
| 226 | return (u_char) cpu_to_le16(readw(this->IO_ADDR_R)); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * nand_write_byte16 - [DEFAULT] write one byte endianess aware to the chip |
| 231 | * @mtd: MTD device structure |
| 232 | * @byte: pointer to data byte to write |
| 233 | * |
| 234 | * Default write function for 16bit buswith with |
| 235 | * endianess conversion |
| 236 | */ |
| 237 | static void nand_write_byte16(struct mtd_info *mtd, u_char byte) |
| 238 | { |
| 239 | struct nand_chip *this = mtd->priv; |
| 240 | writew(le16_to_cpu((u16) byte), this->IO_ADDR_W); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * nand_read_word - [DEFAULT] read one word from the chip |
| 245 | * @mtd: MTD device structure |
| 246 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 247 | * Default read function for 16bit buswith without |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | * endianess conversion |
| 249 | */ |
| 250 | static u16 nand_read_word(struct mtd_info *mtd) |
| 251 | { |
| 252 | struct nand_chip *this = mtd->priv; |
| 253 | return readw(this->IO_ADDR_R); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * nand_write_word - [DEFAULT] write one word to the chip |
| 258 | * @mtd: MTD device structure |
| 259 | * @word: data word to write |
| 260 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 261 | * Default write function for 16bit buswith without |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | * endianess conversion |
| 263 | */ |
| 264 | static void nand_write_word(struct mtd_info *mtd, u16 word) |
| 265 | { |
| 266 | struct nand_chip *this = mtd->priv; |
| 267 | writew(word, this->IO_ADDR_W); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * nand_select_chip - [DEFAULT] control CE line |
| 272 | * @mtd: MTD device structure |
| 273 | * @chip: chipnumber to select, -1 for deselect |
| 274 | * |
| 275 | * Default select function for 1 chip devices. |
| 276 | */ |
| 277 | static void nand_select_chip(struct mtd_info *mtd, int chip) |
| 278 | { |
| 279 | struct nand_chip *this = mtd->priv; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 280 | switch (chip) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | case -1: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 282 | this->hwcontrol(mtd, NAND_CTL_CLRNCE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | break; |
| 284 | case 0: |
| 285 | this->hwcontrol(mtd, NAND_CTL_SETNCE); |
| 286 | break; |
| 287 | |
| 288 | default: |
| 289 | BUG(); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * nand_write_buf - [DEFAULT] write buffer to chip |
| 295 | * @mtd: MTD device structure |
| 296 | * @buf: data buffer |
| 297 | * @len: number of bytes to write |
| 298 | * |
| 299 | * Default write function for 8bit buswith |
| 300 | */ |
| 301 | static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len) |
| 302 | { |
| 303 | int i; |
| 304 | struct nand_chip *this = mtd->priv; |
| 305 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 306 | for (i = 0; i < len; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | writeb(buf[i], this->IO_ADDR_W); |
| 308 | } |
| 309 | |
| 310 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 311 | * nand_read_buf - [DEFAULT] read chip data into buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | * @mtd: MTD device structure |
| 313 | * @buf: buffer to store date |
| 314 | * @len: number of bytes to read |
| 315 | * |
| 316 | * Default read function for 8bit buswith |
| 317 | */ |
| 318 | static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
| 319 | { |
| 320 | int i; |
| 321 | struct nand_chip *this = mtd->priv; |
| 322 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 323 | for (i = 0; i < len; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | buf[i] = readb(this->IO_ADDR_R); |
| 325 | } |
| 326 | |
| 327 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 328 | * nand_verify_buf - [DEFAULT] Verify chip data against buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | * @mtd: MTD device structure |
| 330 | * @buf: buffer containing the data to compare |
| 331 | * @len: number of bytes to compare |
| 332 | * |
| 333 | * Default verify function for 8bit buswith |
| 334 | */ |
| 335 | static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len) |
| 336 | { |
| 337 | int i; |
| 338 | struct nand_chip *this = mtd->priv; |
| 339 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 340 | for (i = 0; i < len; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | if (buf[i] != readb(this->IO_ADDR_R)) |
| 342 | return -EFAULT; |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * nand_write_buf16 - [DEFAULT] write buffer to chip |
| 349 | * @mtd: MTD device structure |
| 350 | * @buf: data buffer |
| 351 | * @len: number of bytes to write |
| 352 | * |
| 353 | * Default write function for 16bit buswith |
| 354 | */ |
| 355 | static void nand_write_buf16(struct mtd_info *mtd, const u_char *buf, int len) |
| 356 | { |
| 357 | int i; |
| 358 | struct nand_chip *this = mtd->priv; |
| 359 | u16 *p = (u16 *) buf; |
| 360 | len >>= 1; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 361 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 362 | for (i = 0; i < len; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | writew(p[i], this->IO_ADDR_W); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 368 | * nand_read_buf16 - [DEFAULT] read chip data into buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | * @mtd: MTD device structure |
| 370 | * @buf: buffer to store date |
| 371 | * @len: number of bytes to read |
| 372 | * |
| 373 | * Default read function for 16bit buswith |
| 374 | */ |
| 375 | static void nand_read_buf16(struct mtd_info *mtd, u_char *buf, int len) |
| 376 | { |
| 377 | int i; |
| 378 | struct nand_chip *this = mtd->priv; |
| 379 | u16 *p = (u16 *) buf; |
| 380 | len >>= 1; |
| 381 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 382 | for (i = 0; i < len; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | p[i] = readw(this->IO_ADDR_R); |
| 384 | } |
| 385 | |
| 386 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 387 | * nand_verify_buf16 - [DEFAULT] Verify chip data against buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | * @mtd: MTD device structure |
| 389 | * @buf: buffer containing the data to compare |
| 390 | * @len: number of bytes to compare |
| 391 | * |
| 392 | * Default verify function for 16bit buswith |
| 393 | */ |
| 394 | static int nand_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len) |
| 395 | { |
| 396 | int i; |
| 397 | struct nand_chip *this = mtd->priv; |
| 398 | u16 *p = (u16 *) buf; |
| 399 | len >>= 1; |
| 400 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 401 | for (i = 0; i < len; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | if (p[i] != readw(this->IO_ADDR_R)) |
| 403 | return -EFAULT; |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * nand_block_bad - [DEFAULT] Read bad block marker from the chip |
| 410 | * @mtd: MTD device structure |
| 411 | * @ofs: offset from device start |
| 412 | * @getchip: 0, if the chip is already selected |
| 413 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 414 | * Check, if the block is bad. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | */ |
| 416 | static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip) |
| 417 | { |
| 418 | int page, chipnr, res = 0; |
| 419 | struct nand_chip *this = mtd->priv; |
| 420 | u16 bad; |
| 421 | |
| 422 | if (getchip) { |
| 423 | page = (int)(ofs >> this->page_shift); |
| 424 | chipnr = (int)(ofs >> this->chip_shift); |
| 425 | |
| 426 | /* Grab the lock and see if the device is available */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 427 | nand_get_device(this, mtd, FL_READING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
| 429 | /* Select the NAND device */ |
| 430 | this->select_chip(mtd, chipnr); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 431 | } else |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 432 | page = (int)ofs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
| 434 | if (this->options & NAND_BUSWIDTH_16) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 435 | this->cmdfunc(mtd, NAND_CMD_READOOB, this->badblockpos & 0xFE, page & this->pagemask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | bad = cpu_to_le16(this->read_word(mtd)); |
| 437 | if (this->badblockpos & 0x1) |
Vitaly Wool | 49196f3 | 2005-11-02 16:54:46 +0000 | [diff] [blame] | 438 | bad >>= 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | if ((bad & 0xFF) != 0xff) |
| 440 | res = 1; |
| 441 | } else { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 442 | this->cmdfunc(mtd, NAND_CMD_READOOB, this->badblockpos, page & this->pagemask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | if (this->read_byte(mtd) != 0xff) |
| 444 | res = 1; |
| 445 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | if (getchip) { |
| 448 | /* Deselect and wake up anyone waiting on the device */ |
| 449 | nand_release_device(mtd); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | return res; |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * nand_default_block_markbad - [DEFAULT] mark a block bad |
| 457 | * @mtd: MTD device structure |
| 458 | * @ofs: offset from device start |
| 459 | * |
| 460 | * This is the default implementation, which can be overridden by |
| 461 | * a hardware specific driver. |
| 462 | */ |
| 463 | static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 464 | { |
| 465 | struct nand_chip *this = mtd->priv; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 466 | u_char buf[2] = { 0, 0 }; |
| 467 | size_t retlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | int block; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | /* Get block number */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 471 | block = ((int)ofs) >> this->bbt_erase_shift; |
Artem B. Bityuckiy | 41ce921 | 2005-02-09 14:50:00 +0000 | [diff] [blame] | 472 | if (this->bbt) |
| 473 | this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
| 475 | /* Do we have a flash based bad block table ? */ |
| 476 | if (this->options & NAND_USE_FLASH_BBT) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 477 | return nand_update_bbt(mtd, ofs); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | /* We write two bytes, so we dont have to mess with 16 bit access */ |
| 480 | ofs += mtd->oobsize + (this->badblockpos & ~0x01); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 481 | return nand_write_oob(mtd, ofs, 2, &retlen, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 484 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | * nand_check_wp - [GENERIC] check if the chip is write protected |
| 486 | * @mtd: MTD device structure |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 487 | * Check, if the device is write protected |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 489 | * The function expects, that the device is already selected |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 491 | static int nand_check_wp(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | { |
| 493 | struct nand_chip *this = mtd->priv; |
| 494 | /* Check the WP bit */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 495 | this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 496 | return (this->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /** |
| 500 | * nand_block_checkbad - [GENERIC] Check if a block is marked bad |
| 501 | * @mtd: MTD device structure |
| 502 | * @ofs: offset from device start |
| 503 | * @getchip: 0, if the chip is already selected |
| 504 | * @allowbbt: 1, if its allowed to access the bbt area |
| 505 | * |
| 506 | * Check, if the block is bad. Either by reading the bad block table or |
| 507 | * calling of the scan function. |
| 508 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 509 | static int nand_block_checkbad(struct mtd_info *mtd, loff_t ofs, int getchip, int allowbbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | { |
| 511 | struct nand_chip *this = mtd->priv; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | if (!this->bbt) |
| 514 | return this->block_bad(mtd, ofs, getchip); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | /* Return info from the table */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 517 | return nand_isbad_bbt(mtd, ofs, allowbbt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 520 | DEFINE_LED_TRIGGER(nand_led_trigger); |
| 521 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 522 | /* |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 523 | * Wait for the ready pin, after a command |
| 524 | * The timeout is catched later. |
| 525 | */ |
| 526 | static void nand_wait_ready(struct mtd_info *mtd) |
| 527 | { |
| 528 | struct nand_chip *this = mtd->priv; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 529 | unsigned long timeo = jiffies + 2; |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 530 | |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 531 | led_trigger_event(nand_led_trigger, LED_FULL); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 532 | /* wait until command is processed or timeout occures */ |
| 533 | do { |
| 534 | if (this->dev_ready(mtd)) |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 535 | break; |
Ingo Molnar | 8446f1d | 2005-09-06 15:16:27 -0700 | [diff] [blame] | 536 | touch_softlockup_watchdog(); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 537 | } while (time_before(jiffies, timeo)); |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 538 | led_trigger_event(nand_led_trigger, LED_OFF); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | /** |
| 542 | * nand_command - [DEFAULT] Send command to NAND device |
| 543 | * @mtd: MTD device structure |
| 544 | * @command: the command to be sent |
| 545 | * @column: the column address for this command, -1 if none |
| 546 | * @page_addr: the page address for this command, -1 if none |
| 547 | * |
| 548 | * Send command to NAND device. This function is used for small page |
| 549 | * devices (256/512 Bytes per page) |
| 550 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 551 | static void nand_command(struct mtd_info *mtd, unsigned command, int column, int page_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | { |
| 553 | register struct nand_chip *this = mtd->priv; |
| 554 | |
| 555 | /* Begin command latch cycle */ |
| 556 | this->hwcontrol(mtd, NAND_CTL_SETCLE); |
| 557 | /* |
| 558 | * Write out the command to the device. |
| 559 | */ |
| 560 | if (command == NAND_CMD_SEQIN) { |
| 561 | int readcmd; |
| 562 | |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 563 | if (column >= mtd->writesize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | /* OOB area */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 565 | column -= mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | readcmd = NAND_CMD_READOOB; |
| 567 | } else if (column < 256) { |
| 568 | /* First 256 bytes --> READ0 */ |
| 569 | readcmd = NAND_CMD_READ0; |
| 570 | } else { |
| 571 | column -= 256; |
| 572 | readcmd = NAND_CMD_READ1; |
| 573 | } |
| 574 | this->write_byte(mtd, readcmd); |
| 575 | } |
| 576 | this->write_byte(mtd, command); |
| 577 | |
| 578 | /* Set ALE and clear CLE to start address cycle */ |
| 579 | this->hwcontrol(mtd, NAND_CTL_CLRCLE); |
| 580 | |
| 581 | if (column != -1 || page_addr != -1) { |
| 582 | this->hwcontrol(mtd, NAND_CTL_SETALE); |
| 583 | |
| 584 | /* Serially input address */ |
| 585 | if (column != -1) { |
| 586 | /* Adjust columns for 16 bit buswidth */ |
| 587 | if (this->options & NAND_BUSWIDTH_16) |
| 588 | column >>= 1; |
| 589 | this->write_byte(mtd, column); |
| 590 | } |
| 591 | if (page_addr != -1) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 592 | this->write_byte(mtd, (unsigned char)(page_addr & 0xff)); |
| 593 | this->write_byte(mtd, (unsigned char)((page_addr >> 8) & 0xff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | /* One more address cycle for devices > 32MiB */ |
| 595 | if (this->chipsize > (32 << 20)) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 596 | this->write_byte(mtd, (unsigned char)((page_addr >> 16) & 0x0f)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | } |
| 598 | /* Latch in address */ |
| 599 | this->hwcontrol(mtd, NAND_CTL_CLRALE); |
| 600 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 601 | |
| 602 | /* |
| 603 | * program and erase have their own busy handlers |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | * status and sequential in needs no delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 605 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | switch (command) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 607 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | case NAND_CMD_PAGEPROG: |
| 609 | case NAND_CMD_ERASE1: |
| 610 | case NAND_CMD_ERASE2: |
| 611 | case NAND_CMD_SEQIN: |
| 612 | case NAND_CMD_STATUS: |
| 613 | return; |
| 614 | |
| 615 | case NAND_CMD_RESET: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 616 | if (this->dev_ready) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | break; |
| 618 | udelay(this->chip_delay); |
| 619 | this->hwcontrol(mtd, NAND_CTL_SETCLE); |
| 620 | this->write_byte(mtd, NAND_CMD_STATUS); |
| 621 | this->hwcontrol(mtd, NAND_CTL_CLRCLE); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 622 | while (!(this->read_byte(mtd) & NAND_STATUS_READY)) ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | return; |
| 624 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 625 | /* This applies to read commands */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | default: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 627 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | * If we don't have access to the busy pin, we apply the given |
| 629 | * command delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 630 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | if (!this->dev_ready) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 632 | udelay(this->chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | return; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 634 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | /* Apply this short delay always to ensure that we do wait tWB in |
| 637 | * any case on any machine. */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 638 | ndelay(100); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 639 | |
| 640 | nand_wait_ready(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /** |
| 644 | * nand_command_lp - [DEFAULT] Send command to NAND large page device |
| 645 | * @mtd: MTD device structure |
| 646 | * @command: the command to be sent |
| 647 | * @column: the column address for this command, -1 if none |
| 648 | * @page_addr: the page address for this command, -1 if none |
| 649 | * |
| 650 | * Send command to NAND device. This is the version for the new large page devices |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 651 | * We dont have the separate regions as we have in the small page devices. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | * We must emulate NAND_CMD_READOOB to keep the code compatible. |
| 653 | * |
| 654 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 655 | static void nand_command_lp(struct mtd_info *mtd, unsigned command, int column, int page_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | { |
| 657 | register struct nand_chip *this = mtd->priv; |
| 658 | |
| 659 | /* Emulate NAND_CMD_READOOB */ |
| 660 | if (command == NAND_CMD_READOOB) { |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 661 | column += mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | command = NAND_CMD_READ0; |
| 663 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 664 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | /* Begin command latch cycle */ |
| 666 | this->hwcontrol(mtd, NAND_CTL_SETCLE); |
| 667 | /* Write out the command to the device. */ |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 668 | this->write_byte(mtd, (command & 0xff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | /* End command latch cycle */ |
| 670 | this->hwcontrol(mtd, NAND_CTL_CLRCLE); |
| 671 | |
| 672 | if (column != -1 || page_addr != -1) { |
| 673 | this->hwcontrol(mtd, NAND_CTL_SETALE); |
| 674 | |
| 675 | /* Serially input address */ |
| 676 | if (column != -1) { |
| 677 | /* Adjust columns for 16 bit buswidth */ |
| 678 | if (this->options & NAND_BUSWIDTH_16) |
| 679 | column >>= 1; |
| 680 | this->write_byte(mtd, column & 0xff); |
| 681 | this->write_byte(mtd, column >> 8); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 682 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | if (page_addr != -1) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 684 | this->write_byte(mtd, (unsigned char)(page_addr & 0xff)); |
| 685 | this->write_byte(mtd, (unsigned char)((page_addr >> 8) & 0xff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | /* One more address cycle for devices > 128MiB */ |
| 687 | if (this->chipsize > (128 << 20)) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 688 | this->write_byte(mtd, (unsigned char)((page_addr >> 16) & 0xff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | } |
| 690 | /* Latch in address */ |
| 691 | this->hwcontrol(mtd, NAND_CTL_CLRALE); |
| 692 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 693 | |
| 694 | /* |
| 695 | * program and erase have their own busy handlers |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 696 | * status, sequential in, and deplete1 need no delay |
| 697 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | switch (command) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 699 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | case NAND_CMD_CACHEDPROG: |
| 701 | case NAND_CMD_PAGEPROG: |
| 702 | case NAND_CMD_ERASE1: |
| 703 | case NAND_CMD_ERASE2: |
| 704 | case NAND_CMD_SEQIN: |
| 705 | case NAND_CMD_STATUS: |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 706 | case NAND_CMD_DEPLETE1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | return; |
| 708 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 709 | /* |
| 710 | * read error status commands require only a short delay |
| 711 | */ |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 712 | case NAND_CMD_STATUS_ERROR: |
| 713 | case NAND_CMD_STATUS_ERROR0: |
| 714 | case NAND_CMD_STATUS_ERROR1: |
| 715 | case NAND_CMD_STATUS_ERROR2: |
| 716 | case NAND_CMD_STATUS_ERROR3: |
| 717 | udelay(this->chip_delay); |
| 718 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | |
| 720 | case NAND_CMD_RESET: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 721 | if (this->dev_ready) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | break; |
| 723 | udelay(this->chip_delay); |
| 724 | this->hwcontrol(mtd, NAND_CTL_SETCLE); |
| 725 | this->write_byte(mtd, NAND_CMD_STATUS); |
| 726 | this->hwcontrol(mtd, NAND_CTL_CLRCLE); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 727 | while (!(this->read_byte(mtd) & NAND_STATUS_READY)) ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | return; |
| 729 | |
| 730 | case NAND_CMD_READ0: |
| 731 | /* Begin command latch cycle */ |
| 732 | this->hwcontrol(mtd, NAND_CTL_SETCLE); |
| 733 | /* Write out the start read command */ |
| 734 | this->write_byte(mtd, NAND_CMD_READSTART); |
| 735 | /* End command latch cycle */ |
| 736 | this->hwcontrol(mtd, NAND_CTL_CLRCLE); |
| 737 | /* Fall through into ready check */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 738 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 739 | /* This applies to read commands */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | default: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 741 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | * If we don't have access to the busy pin, we apply the given |
| 743 | * command delay |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 744 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | if (!this->dev_ready) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 746 | udelay(this->chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | return; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 748 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | } |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 750 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | /* Apply this short delay always to ensure that we do wait tWB in |
| 752 | * any case on any machine. */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 753 | ndelay(100); |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 754 | |
| 755 | nand_wait_ready(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | /** |
| 759 | * nand_get_device - [GENERIC] Get chip for selected access |
| 760 | * @this: the nand chip descriptor |
| 761 | * @mtd: MTD device structure |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 762 | * @new_state: the state which is requested |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | * |
| 764 | * Get the device and lock it for exclusive access |
| 765 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 766 | static int nand_get_device(struct nand_chip *this, struct mtd_info *mtd, int new_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | { |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 768 | struct nand_chip *active; |
| 769 | spinlock_t *lock; |
| 770 | wait_queue_head_t *wq; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 771 | DECLARE_WAITQUEUE(wait, current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 773 | lock = (this->controller) ? &this->controller->lock : &this->chip_lock; |
| 774 | wq = (this->controller) ? &this->controller->wq : &this->wq; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 775 | retry: |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 776 | active = this; |
| 777 | spin_lock(lock); |
| 778 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | /* Hardware controller shared among independend devices */ |
| 780 | if (this->controller) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | if (this->controller->active) |
| 782 | active = this->controller->active; |
| 783 | else |
| 784 | this->controller->active = this; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | } |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 786 | if (active == this && this->state == FL_READY) { |
| 787 | this->state = new_state; |
| 788 | spin_unlock(lock); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 789 | return 0; |
| 790 | } |
| 791 | if (new_state == FL_PM_SUSPENDED) { |
| 792 | spin_unlock(lock); |
| 793 | return (this->state == FL_PM_SUSPENDED) ? 0 : -EAGAIN; |
Thomas Gleixner | 0dfc624 | 2005-05-31 20:39:20 +0100 | [diff] [blame] | 794 | } |
| 795 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 796 | add_wait_queue(wq, &wait); |
| 797 | spin_unlock(lock); |
| 798 | schedule(); |
| 799 | remove_wait_queue(wq, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | goto retry; |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * nand_wait - [DEFAULT] wait until the command is done |
| 805 | * @mtd: MTD device structure |
| 806 | * @this: NAND chip structure |
| 807 | * @state: state to select the max. timeout value |
| 808 | * |
| 809 | * Wait for command done. This applies to erase and program only |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 810 | * 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] | 811 | * general NAND and SmartMedia specs |
| 812 | * |
| 813 | */ |
| 814 | static int nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state) |
| 815 | { |
| 816 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 817 | unsigned long timeo = jiffies; |
| 818 | int status; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 819 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | if (state == FL_ERASING) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 821 | timeo += (HZ * 400) / 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | else |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 823 | timeo += (HZ * 20) / 1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 825 | led_trigger_event(nand_led_trigger, LED_FULL); |
| 826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | /* Apply this short delay always to ensure that we do wait tWB in |
| 828 | * any case on any machine. */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 829 | ndelay(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | |
| 831 | if ((state == FL_ERASING) && (this->options & NAND_IS_AND)) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 832 | this->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 833 | else |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 834 | this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 836 | while (time_before(jiffies, timeo)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | /* Check, if we were interrupted */ |
| 838 | if (this->state != state) |
| 839 | return 0; |
| 840 | |
| 841 | if (this->dev_ready) { |
| 842 | if (this->dev_ready(mtd)) |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 843 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | } else { |
| 845 | if (this->read_byte(mtd) & NAND_STATUS_READY) |
| 846 | break; |
| 847 | } |
Thomas Gleixner | 20a6c21 | 2005-03-01 09:32:48 +0000 | [diff] [blame] | 848 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | } |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 850 | led_trigger_event(nand_led_trigger, LED_OFF); |
| 851 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 852 | status = (int)this->read_byte(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | return status; |
| 854 | } |
| 855 | |
| 856 | /** |
| 857 | * nand_write_page - [GENERIC] write one page |
| 858 | * @mtd: MTD device structure |
| 859 | * @this: NAND chip structure |
| 860 | * @page: startpage inside the chip, must be called with (page & this->pagemask) |
| 861 | * @oob_buf: out of band data buffer |
| 862 | * @oobsel: out of band selecttion structre |
| 863 | * @cached: 1 = enable cached programming if supported by chip |
| 864 | * |
| 865 | * Nand_page_program function is used for write and writev ! |
| 866 | * This function will always program a full page of data |
| 867 | * If you call it with a non page aligned buffer, you're lost :) |
| 868 | * |
| 869 | * Cached programming is not supported yet. |
| 870 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 871 | static int nand_write_page(struct mtd_info *mtd, struct nand_chip *this, int page, |
| 872 | u_char *oob_buf, struct nand_oobinfo *oobsel, int cached) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 874 | int i, status; |
| 875 | u_char ecc_code[32]; |
| 876 | int eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE; |
| 877 | int *oob_config = oobsel->eccpos; |
| 878 | int datidx = 0, eccidx = 0, eccsteps = this->eccsteps; |
| 879 | int eccbytes = 0; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 880 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | /* FIXME: Enable cached programming */ |
| 882 | cached = 0; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 883 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | /* Send command to begin auto page programming */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 885 | this->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
| 887 | /* Write out complete page of data, take care of eccmode */ |
| 888 | switch (eccmode) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 889 | /* No ecc, write all */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | case NAND_ECC_NONE: |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 891 | printk(KERN_WARNING "Writing data without ECC to NAND-FLASH is not recommended\n"); |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 892 | this->write_buf(mtd, this->data_poi, mtd->writesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | break; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 894 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 895 | /* Software ecc 3/256, write all */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | case NAND_ECC_SOFT: |
| 897 | for (; eccsteps; eccsteps--) { |
| 898 | this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code); |
| 899 | for (i = 0; i < 3; i++, eccidx++) |
| 900 | oob_buf[oob_config[eccidx]] = ecc_code[i]; |
| 901 | datidx += this->eccsize; |
| 902 | } |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 903 | this->write_buf(mtd, this->data_poi, mtd->writesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | break; |
| 905 | default: |
| 906 | eccbytes = this->eccbytes; |
| 907 | for (; eccsteps; eccsteps--) { |
| 908 | /* enable hardware ecc logic for write */ |
| 909 | this->enable_hwecc(mtd, NAND_ECC_WRITE); |
| 910 | this->write_buf(mtd, &this->data_poi[datidx], this->eccsize); |
| 911 | this->calculate_ecc(mtd, &this->data_poi[datidx], ecc_code); |
| 912 | for (i = 0; i < eccbytes; i++, eccidx++) |
| 913 | oob_buf[oob_config[eccidx]] = ecc_code[i]; |
| 914 | /* If the hardware ecc provides syndromes then |
| 915 | * the ecc code must be written immidiately after |
| 916 | * the data bytes (words) */ |
| 917 | if (this->options & NAND_HWECC_SYNDROME) |
| 918 | this->write_buf(mtd, ecc_code, eccbytes); |
| 919 | datidx += this->eccsize; |
| 920 | } |
| 921 | break; |
| 922 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 923 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | /* Write out OOB data */ |
| 925 | if (this->options & NAND_HWECC_SYNDROME) |
| 926 | this->write_buf(mtd, &oob_buf[oobsel->eccbytes], mtd->oobsize - oobsel->eccbytes); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 927 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | this->write_buf(mtd, oob_buf, mtd->oobsize); |
| 929 | |
| 930 | /* Send command to actually program the data */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 931 | this->cmdfunc(mtd, cached ? NAND_CMD_CACHEDPROG : NAND_CMD_PAGEPROG, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | |
| 933 | if (!cached) { |
| 934 | /* call wait ready function */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 935 | status = this->waitfunc(mtd, this, FL_WRITING); |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 936 | |
| 937 | /* See if operation failed and additional status checks are available */ |
| 938 | if ((status & NAND_STATUS_FAIL) && (this->errstat)) { |
| 939 | status = this->errstat(mtd, this, FL_WRITING, status, page); |
| 940 | } |
| 941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | /* See if device thinks it succeeded */ |
David A. Marlin | a4ab4c5 | 2005-01-23 18:30:53 +0000 | [diff] [blame] | 943 | if (status & NAND_STATUS_FAIL) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 944 | DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write, page 0x%08x, ", __FUNCTION__, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | return -EIO; |
| 946 | } |
| 947 | } else { |
| 948 | /* FIXME: Implement cached programming ! */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 949 | /* wait until cache is ready */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | // status = this->waitfunc (mtd, this, FL_CACHEDRPG); |
| 951 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 952 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | #ifdef CONFIG_MTD_NAND_VERIFY_WRITE |
| 956 | /** |
| 957 | * nand_verify_pages - [GENERIC] verify the chip contents after a write |
| 958 | * @mtd: MTD device structure |
| 959 | * @this: NAND chip structure |
| 960 | * @page: startpage inside the chip, must be called with (page & this->pagemask) |
| 961 | * @numpages: number of pages to verify |
| 962 | * @oob_buf: out of band data buffer |
| 963 | * @oobsel: out of band selecttion structre |
| 964 | * @chipnr: number of the current chip |
| 965 | * @oobmode: 1 = full buffer verify, 0 = ecc only |
| 966 | * |
| 967 | * The NAND device assumes that it is always writing to a cleanly erased page. |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 968 | * Hence, it performs its internal write verification only on bits that |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | * transitioned from 1 to 0. The device does NOT verify the whole page on a |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 970 | * byte by byte basis. It is possible that the page was not completely erased |
| 971 | * or the page is becoming unusable due to wear. The read with ECC would catch |
| 972 | * the error later when the ECC page check fails, but we would rather catch |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | * it early in the page write stage. Better to write no data than invalid data. |
| 974 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 975 | static int nand_verify_pages(struct mtd_info *mtd, struct nand_chip *this, int page, int numpages, |
| 976 | u_char *oob_buf, struct nand_oobinfo *oobsel, int chipnr, int oobmode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 978 | int i, j, datidx = 0, oobofs = 0, res = -EIO; |
| 979 | int eccsteps = this->eccsteps; |
| 980 | int hweccbytes; |
| 981 | u_char oobdata[64]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | |
| 983 | hweccbytes = (this->options & NAND_HWECC_SYNDROME) ? (oobsel->eccbytes / eccsteps) : 0; |
| 984 | |
| 985 | /* Send command to read back the first page */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 986 | this->cmdfunc(mtd, NAND_CMD_READ0, 0, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 988 | for (;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | for (j = 0; j < eccsteps; j++) { |
| 990 | /* Loop through and verify the data */ |
| 991 | if (this->verify_buf(mtd, &this->data_poi[datidx], mtd->eccsize)) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 992 | DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | goto out; |
| 994 | } |
| 995 | datidx += mtd->eccsize; |
| 996 | /* Have we a hw generator layout ? */ |
| 997 | if (!hweccbytes) |
| 998 | continue; |
| 999 | if (this->verify_buf(mtd, &this->oob_buf[oobofs], hweccbytes)) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1000 | DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | goto out; |
| 1002 | } |
| 1003 | oobofs += hweccbytes; |
| 1004 | } |
| 1005 | |
| 1006 | /* check, if we must compare all data or if we just have to |
| 1007 | * compare the ecc bytes |
| 1008 | */ |
| 1009 | if (oobmode) { |
| 1010 | if (this->verify_buf(mtd, &oob_buf[oobofs], mtd->oobsize - hweccbytes * eccsteps)) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1011 | DEBUG(MTD_DEBUG_LEVEL0, "%s: " "Failed write verify, page 0x%08x ", __FUNCTION__, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | goto out; |
| 1013 | } |
| 1014 | } else { |
| 1015 | /* Read always, else autoincrement fails */ |
| 1016 | this->read_buf(mtd, oobdata, mtd->oobsize - hweccbytes * eccsteps); |
| 1017 | |
| 1018 | if (oobsel->useecc != MTD_NANDECC_OFF && !hweccbytes) { |
| 1019 | int ecccnt = oobsel->eccbytes; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1020 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | for (i = 0; i < ecccnt; i++) { |
| 1022 | int idx = oobsel->eccpos[i]; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1023 | if (oobdata[idx] != oob_buf[oobofs + idx]) { |
| 1024 | DEBUG(MTD_DEBUG_LEVEL0, "%s: Failed ECC write verify, page 0x%08x, %6i bytes were succesful\n", |
| 1025 | __FUNCTION__, page, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | goto out; |
| 1027 | } |
| 1028 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1029 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | } |
| 1031 | oobofs += mtd->oobsize - hweccbytes * eccsteps; |
| 1032 | page++; |
| 1033 | numpages--; |
| 1034 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1035 | /* Apply delay or wait for ready/busy pin |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | * Do this before the AUTOINCR check, so no problems |
| 1037 | * arise if a chip which does auto increment |
| 1038 | * is marked as NOAUTOINCR by the board driver. |
| 1039 | * Do this also before returning, so the chip is |
| 1040 | * ready for the next command. |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1041 | */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1042 | if (!this->dev_ready) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1043 | udelay(this->chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | else |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 1045 | nand_wait_ready(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | |
| 1047 | /* All done, return happy */ |
| 1048 | if (!numpages) |
| 1049 | return 0; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1050 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1051 | /* Check, if the chip supports auto page increment */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | if (!NAND_CANAUTOINCR(this)) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1053 | this->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1055 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | * Terminate the read command. We come here in case of an error |
| 1057 | * So we must issue a reset command. |
| 1058 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1059 | out: |
| 1060 | this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | return res; |
| 1062 | } |
| 1063 | #endif |
| 1064 | |
| 1065 | /** |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1066 | * nand_read - [MTD Interface] MTD compability function for nand_do_read_ecc |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | * @mtd: MTD device structure |
| 1068 | * @from: offset to read from |
| 1069 | * @len: number of bytes to read |
| 1070 | * @retlen: pointer to variable to store the number of read bytes |
| 1071 | * @buf: the databuffer to put data |
| 1072 | * |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1073 | * This function simply calls nand_do_read_ecc with oob buffer and oobsel = NULL |
| 1074 | * and flags = 0xff |
| 1075 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1076 | static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1078 | return nand_do_read_ecc(mtd, from, len, retlen, buf, NULL, &mtd->oobinfo, 0xff); |
Thomas Gleixner | 22c60f5 | 2005-04-04 19:56:32 +0100 | [diff] [blame] | 1079 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | /** |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1082 | * nand_read_ecc - [MTD Interface] MTD compability function for nand_do_read_ecc |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | * @mtd: MTD device structure |
| 1084 | * @from: offset to read from |
| 1085 | * @len: number of bytes to read |
| 1086 | * @retlen: pointer to variable to store the number of read bytes |
| 1087 | * @buf: the databuffer to put data |
| 1088 | * @oob_buf: filesystem supplied oob data buffer |
| 1089 | * @oobsel: oob selection structure |
| 1090 | * |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1091 | * This function simply calls nand_do_read_ecc with flags = 0xff |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1093 | static int nand_read_ecc(struct mtd_info *mtd, loff_t from, size_t len, |
| 1094 | size_t *retlen, u_char *buf, u_char *oob_buf, struct nand_oobinfo *oobsel) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | { |
Thomas Gleixner | 22c60f5 | 2005-04-04 19:56:32 +0100 | [diff] [blame] | 1096 | /* use userspace supplied oobinfo, if zero */ |
| 1097 | if (oobsel == NULL) |
| 1098 | oobsel = &mtd->oobinfo; |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1099 | return nand_do_read_ecc(mtd, from, len, retlen, buf, oob_buf, oobsel, 0xff); |
| 1100 | } |
| 1101 | |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1102 | /** |
| 1103 | * nand_do_read_ecc - [MTD Interface] Read data with ECC |
| 1104 | * @mtd: MTD device structure |
| 1105 | * @from: offset to read from |
| 1106 | * @len: number of bytes to read |
| 1107 | * @retlen: pointer to variable to store the number of read bytes |
| 1108 | * @buf: the databuffer to put data |
Dan Brown | bb75ba4 | 2005-04-04 19:02:26 +0100 | [diff] [blame] | 1109 | * @oob_buf: filesystem supplied oob data buffer (can be NULL) |
Thomas Gleixner | 22c60f5 | 2005-04-04 19:56:32 +0100 | [diff] [blame] | 1110 | * @oobsel: oob selection structure |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1111 | * @flags: flag to indicate if nand_get_device/nand_release_device should be preformed |
| 1112 | * and how many corrected error bits are acceptable: |
| 1113 | * bits 0..7 - number of tolerable errors |
| 1114 | * bit 8 - 0 == do not get/release chip, 1 == get/release chip |
| 1115 | * |
| 1116 | * NAND read with ECC |
| 1117 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1118 | int nand_do_read_ecc(struct mtd_info *mtd, loff_t from, size_t len, |
| 1119 | size_t *retlen, u_char *buf, u_char *oob_buf, struct nand_oobinfo *oobsel, int flags) |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1120 | { |
Thomas Gleixner | 22c60f5 | 2005-04-04 19:56:32 +0100 | [diff] [blame] | 1121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | int i, j, col, realpage, page, end, ecc, chipnr, sndcmd = 1; |
| 1123 | int read = 0, oob = 0, ecc_status = 0, ecc_failed = 0; |
| 1124 | struct nand_chip *this = mtd->priv; |
| 1125 | u_char *data_poi, *oob_data = oob_buf; |
Jarkko Lavinen | 0a18cde | 2005-04-11 15:16:11 +0100 | [diff] [blame] | 1126 | u_char ecc_calc[32]; |
| 1127 | u_char ecc_code[32]; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1128 | int eccmode, eccsteps; |
| 1129 | int *oob_config, datidx; |
| 1130 | int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1; |
| 1131 | int eccbytes; |
| 1132 | int compareecc = 1; |
| 1133 | int oobreadlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1135 | DEBUG(MTD_DEBUG_LEVEL3, "nand_read_ecc: from = 0x%08x, len = %i\n", (unsigned int)from, (int)len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | |
| 1137 | /* Do not allow reads past end of device */ |
| 1138 | if ((from + len) > mtd->size) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1139 | DEBUG(MTD_DEBUG_LEVEL0, "nand_read_ecc: Attempt read beyond end of device\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | *retlen = 0; |
| 1141 | return -EINVAL; |
| 1142 | } |
| 1143 | |
| 1144 | /* Grab the lock and see if the device is available */ |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1145 | if (flags & NAND_GET_DEVICE) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1146 | nand_get_device(this, mtd, FL_READING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | /* Autoplace of oob data ? Use the default placement scheme */ |
| 1149 | if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) |
| 1150 | oobsel = this->autooob; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | eccmode = oobsel->useecc ? this->eccmode : NAND_ECC_NONE; |
| 1153 | oob_config = oobsel->eccpos; |
| 1154 | |
| 1155 | /* Select the NAND device */ |
| 1156 | chipnr = (int)(from >> this->chip_shift); |
| 1157 | this->select_chip(mtd, chipnr); |
| 1158 | |
| 1159 | /* First we calculate the starting page */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1160 | realpage = (int)(from >> this->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | page = realpage & this->pagemask; |
| 1162 | |
| 1163 | /* Get raw starting column */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 1164 | col = from & (mtd->writesize - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 1166 | end = mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | ecc = this->eccsize; |
| 1168 | eccbytes = this->eccbytes; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | if ((eccmode == NAND_ECC_NONE) || (this->options & NAND_HWECC_SYNDROME)) |
| 1171 | compareecc = 0; |
| 1172 | |
| 1173 | oobreadlen = mtd->oobsize; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1174 | if (this->options & NAND_HWECC_SYNDROME) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | oobreadlen -= oobsel->eccbytes; |
| 1176 | |
| 1177 | /* Loop until all data read */ |
| 1178 | while (read < len) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | int aligned = (!col && (len - read) >= end); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1181 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | * If the read is not page aligned, we have to read into data buffer |
| 1183 | * due to ecc, else we read into return buffer direct |
| 1184 | */ |
| 1185 | if (aligned) |
| 1186 | data_poi = &buf[read]; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1187 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | data_poi = this->data_buf; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1189 | |
| 1190 | /* Check, if we have this page in the buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | * |
| 1192 | * FIXME: Make it work when we must provide oob data too, |
| 1193 | * check the usage of data_buf oob field |
| 1194 | */ |
| 1195 | if (realpage == this->pagebuf && !oob_buf) { |
| 1196 | /* aligned read ? */ |
| 1197 | if (aligned) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1198 | memcpy(data_poi, this->data_buf, end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | goto readdata; |
| 1200 | } |
| 1201 | |
| 1202 | /* Check, if we must send the read command */ |
| 1203 | if (sndcmd) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1204 | this->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | sndcmd = 0; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1206 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | |
| 1208 | /* get oob area, if we have no oob buffer from fs-driver */ |
Thomas Gleixner | 90e260c | 2005-05-19 17:10:26 +0100 | [diff] [blame] | 1209 | if (!oob_buf || oobsel->useecc == MTD_NANDECC_AUTOPLACE || |
| 1210 | oobsel->useecc == MTD_NANDECC_AUTOPL_USR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | oob_data = &this->data_buf[end]; |
| 1212 | |
| 1213 | eccsteps = this->eccsteps; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | switch (eccmode) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1216 | case NAND_ECC_NONE:{ |
| 1217 | /* No ECC, Read in a page */ |
| 1218 | static unsigned long lastwhinge = 0; |
| 1219 | if ((lastwhinge / HZ) != (jiffies / HZ)) { |
| 1220 | printk(KERN_WARNING |
| 1221 | "Reading data from NAND FLASH without ECC is not recommended\n"); |
| 1222 | lastwhinge = jiffies; |
| 1223 | } |
| 1224 | this->read_buf(mtd, data_poi, end); |
| 1225 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | case NAND_ECC_SOFT: /* Software ECC 3/256: Read in a page + oob data */ |
| 1229 | this->read_buf(mtd, data_poi, end); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1230 | for (i = 0, datidx = 0; eccsteps; eccsteps--, i += 3, datidx += ecc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1232 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | |
| 1234 | default: |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1235 | for (i = 0, datidx = 0; eccsteps; eccsteps--, i += eccbytes, datidx += ecc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | this->enable_hwecc(mtd, NAND_ECC_READ); |
| 1237 | this->read_buf(mtd, &data_poi[datidx], ecc); |
| 1238 | |
| 1239 | /* HW ecc with syndrome calculation must read the |
| 1240 | * syndrome from flash immidiately after the data */ |
| 1241 | if (!compareecc) { |
| 1242 | /* Some hw ecc generators need to know when the |
| 1243 | * syndrome is read from flash */ |
| 1244 | this->enable_hwecc(mtd, NAND_ECC_READSYN); |
| 1245 | this->read_buf(mtd, &oob_data[i], eccbytes); |
| 1246 | /* We calc error correction directly, it checks the hw |
| 1247 | * generator for an error, reads back the syndrome and |
| 1248 | * does the error correction on the fly */ |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1249 | ecc_status = this->correct_data(mtd, &data_poi[datidx], &oob_data[i], &ecc_code[i]); |
| 1250 | if ((ecc_status == -1) || (ecc_status > (flags && 0xff))) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1251 | DEBUG(MTD_DEBUG_LEVEL0, "nand_read_ecc: " |
| 1252 | "Failed ECC read, page 0x%08x on chip %d\n", page, chipnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | ecc_failed++; |
| 1254 | } |
| 1255 | } else { |
| 1256 | this->calculate_ecc(mtd, &data_poi[datidx], &ecc_calc[i]); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1259 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | /* read oobdata */ |
| 1263 | this->read_buf(mtd, &oob_data[mtd->oobsize - oobreadlen], oobreadlen); |
| 1264 | |
| 1265 | /* Skip ECC check, if not requested (ECC_NONE or HW_ECC with syndromes) */ |
| 1266 | if (!compareecc) |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1267 | goto readoob; |
| 1268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | /* Pick the ECC bytes out of the oob data */ |
| 1270 | for (j = 0; j < oobsel->eccbytes; j++) |
| 1271 | ecc_code[j] = oob_data[oob_config[j]]; |
| 1272 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1273 | /* correct data, if necessary */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | for (i = 0, j = 0, datidx = 0; i < this->eccsteps; i++, datidx += ecc) { |
| 1275 | ecc_status = this->correct_data(mtd, &data_poi[datidx], &ecc_code[j], &ecc_calc[j]); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | /* Get next chunk of ecc bytes */ |
| 1278 | j += eccbytes; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1279 | |
| 1280 | /* Check, if we have a fs supplied oob-buffer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | * This is the legacy mode. Used by YAFFS1 |
| 1282 | * Should go away some day |
| 1283 | */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1284 | if (oob_buf && oobsel->useecc == MTD_NANDECC_PLACE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | int *p = (int *)(&oob_data[mtd->oobsize]); |
| 1286 | p[i] = ecc_status; |
| 1287 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1288 | |
| 1289 | if ((ecc_status == -1) || (ecc_status > (flags && 0xff))) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1290 | DEBUG(MTD_DEBUG_LEVEL0, "nand_read_ecc: " "Failed ECC read, page 0x%08x\n", page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | ecc_failed++; |
| 1292 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1293 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1295 | readoob: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | /* check, if we have a fs supplied oob-buffer */ |
| 1297 | if (oob_buf) { |
| 1298 | /* without autoplace. Legacy mode used by YAFFS1 */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1299 | switch (oobsel->useecc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | case MTD_NANDECC_AUTOPLACE: |
Thomas Gleixner | 90e260c | 2005-05-19 17:10:26 +0100 | [diff] [blame] | 1301 | case MTD_NANDECC_AUTOPL_USR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | /* Walk through the autoplace chunks */ |
Dan Brown | 82e1d19 | 2005-04-06 21:13:09 +0100 | [diff] [blame] | 1303 | for (i = 0; oobsel->oobfree[i][1]; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | int from = oobsel->oobfree[i][0]; |
| 1305 | int num = oobsel->oobfree[i][1]; |
| 1306 | memcpy(&oob_buf[oob], &oob_data[from], num); |
Dan Brown | 82e1d19 | 2005-04-06 21:13:09 +0100 | [diff] [blame] | 1307 | oob += num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | break; |
| 1310 | case MTD_NANDECC_PLACE: |
| 1311 | /* YAFFS1 legacy mode */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1312 | oob_data += this->eccsteps * sizeof(int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | default: |
| 1314 | oob_data += mtd->oobsize; |
| 1315 | } |
| 1316 | } |
| 1317 | readdata: |
| 1318 | /* Partial page read, transfer data into fs buffer */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1319 | if (!aligned) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | for (j = col; j < end && read < len; j++) |
| 1321 | buf[read++] = data_poi[j]; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1322 | this->pagebuf = realpage; |
| 1323 | } else |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 1324 | read += mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1326 | /* Apply delay or wait for ready/busy pin |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | * Do this before the AUTOINCR check, so no problems |
| 1328 | * arise if a chip which does auto increment |
| 1329 | * is marked as NOAUTOINCR by the board driver. |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1330 | */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1331 | if (!this->dev_ready) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1332 | udelay(this->chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | else |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 1334 | nand_wait_ready(mtd); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | if (read == len) |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1337 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | |
| 1339 | /* For subsequent reads align to page boundary. */ |
| 1340 | col = 0; |
| 1341 | /* Increment page address */ |
| 1342 | realpage++; |
| 1343 | |
| 1344 | page = realpage & this->pagemask; |
| 1345 | /* Check, if we cross a chip boundary */ |
| 1346 | if (!page) { |
| 1347 | chipnr++; |
| 1348 | this->select_chip(mtd, -1); |
| 1349 | this->select_chip(mtd, chipnr); |
| 1350 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1351 | /* Check, if the chip supports auto page increment |
| 1352 | * or if we have hit a block boundary. |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1353 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1355 | sndcmd = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | /* Deselect and wake up anyone waiting on the device */ |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 1359 | if (flags & NAND_GET_DEVICE) |
| 1360 | nand_release_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | |
| 1362 | /* |
| 1363 | * Return success, if no ECC failures, else -EBADMSG |
| 1364 | * fs driver will take care of that, because |
| 1365 | * retlen == desired len and result == -EBADMSG |
| 1366 | */ |
| 1367 | *retlen = read; |
| 1368 | return ecc_failed ? -EBADMSG : 0; |
| 1369 | } |
| 1370 | |
| 1371 | /** |
| 1372 | * nand_read_oob - [MTD Interface] NAND read out-of-band |
| 1373 | * @mtd: MTD device structure |
| 1374 | * @from: offset to read from |
| 1375 | * @len: number of bytes to read |
| 1376 | * @retlen: pointer to variable to store the number of read bytes |
| 1377 | * @buf: the databuffer to put data |
| 1378 | * |
| 1379 | * NAND read out-of-band data from the spare area |
| 1380 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1381 | static int nand_read_oob(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | { |
| 1383 | int i, col, page, chipnr; |
| 1384 | struct nand_chip *this = mtd->priv; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1385 | int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1387 | DEBUG(MTD_DEBUG_LEVEL3, "nand_read_oob: from = 0x%08x, len = %i\n", (unsigned int)from, (int)len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | |
| 1389 | /* Shift to get page */ |
| 1390 | page = (int)(from >> this->page_shift); |
| 1391 | chipnr = (int)(from >> this->chip_shift); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | /* Mask to get column */ |
| 1394 | col = from & (mtd->oobsize - 1); |
| 1395 | |
| 1396 | /* Initialize return length value */ |
| 1397 | *retlen = 0; |
| 1398 | |
| 1399 | /* Do not allow reads past end of device */ |
| 1400 | if ((from + len) > mtd->size) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1401 | DEBUG(MTD_DEBUG_LEVEL0, "nand_read_oob: Attempt read beyond end of device\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | *retlen = 0; |
| 1403 | return -EINVAL; |
| 1404 | } |
| 1405 | |
| 1406 | /* Grab the lock and see if the device is available */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1407 | nand_get_device(this, mtd, FL_READING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | |
| 1409 | /* Select the NAND device */ |
| 1410 | this->select_chip(mtd, chipnr); |
| 1411 | |
| 1412 | /* Send the read command */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1413 | this->cmdfunc(mtd, NAND_CMD_READOOB, col, page & this->pagemask); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1414 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | * Read the data, if we read more than one page |
| 1416 | * oob data, let the device transfer the data ! |
| 1417 | */ |
| 1418 | i = 0; |
| 1419 | while (i < len) { |
| 1420 | int thislen = mtd->oobsize - col; |
| 1421 | thislen = min_t(int, thislen, len); |
| 1422 | this->read_buf(mtd, &buf[i], thislen); |
| 1423 | i += thislen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | |
| 1425 | /* Read more ? */ |
| 1426 | if (i < len) { |
| 1427 | page++; |
| 1428 | col = 0; |
| 1429 | |
| 1430 | /* Check, if we cross a chip boundary */ |
| 1431 | if (!(page & this->pagemask)) { |
| 1432 | chipnr++; |
| 1433 | this->select_chip(mtd, -1); |
| 1434 | this->select_chip(mtd, chipnr); |
| 1435 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1436 | |
| 1437 | /* Apply delay or wait for ready/busy pin |
Thomas Gleixner | 19870da | 2005-07-15 14:53:51 +0100 | [diff] [blame] | 1438 | * Do this before the AUTOINCR check, so no problems |
| 1439 | * arise if a chip which does auto increment |
| 1440 | * is marked as NOAUTOINCR by the board driver. |
| 1441 | */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1442 | if (!this->dev_ready) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1443 | udelay(this->chip_delay); |
Thomas Gleixner | 19870da | 2005-07-15 14:53:51 +0100 | [diff] [blame] | 1444 | else |
| 1445 | nand_wait_ready(mtd); |
| 1446 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1447 | /* Check, if the chip supports auto page increment |
| 1448 | * or if we have hit a block boundary. |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1449 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) { |
| 1451 | /* For subsequent page reads set offset to 0 */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1452 | this->cmdfunc(mtd, NAND_CMD_READOOB, 0x0, page & this->pagemask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | } |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | /* Deselect and wake up anyone waiting on the device */ |
| 1458 | nand_release_device(mtd); |
| 1459 | |
| 1460 | /* Return happy */ |
| 1461 | *retlen = len; |
| 1462 | return 0; |
| 1463 | } |
| 1464 | |
| 1465 | /** |
| 1466 | * nand_read_raw - [GENERIC] Read raw data including oob into buffer |
| 1467 | * @mtd: MTD device structure |
| 1468 | * @buf: temporary buffer |
| 1469 | * @from: offset to read from |
| 1470 | * @len: number of bytes to read |
| 1471 | * @ooblen: number of oob data bytes to read |
| 1472 | * |
| 1473 | * Read raw data including oob into buffer |
| 1474 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1475 | int nand_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t from, size_t len, size_t ooblen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | { |
| 1477 | struct nand_chip *this = mtd->priv; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1478 | int page = (int)(from >> this->page_shift); |
| 1479 | int chip = (int)(from >> this->chip_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | int sndcmd = 1; |
| 1481 | int cnt = 0; |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 1482 | int pagesize = mtd->writesize + mtd->oobsize; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1483 | int blockcheck = (1 << (this->phys_erase_shift - this->page_shift)) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | |
| 1485 | /* Do not allow reads past end of device */ |
| 1486 | if ((from + len) > mtd->size) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1487 | DEBUG(MTD_DEBUG_LEVEL0, "nand_read_raw: Attempt read beyond end of device\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | return -EINVAL; |
| 1489 | } |
| 1490 | |
| 1491 | /* Grab the lock and see if the device is available */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1492 | nand_get_device(this, mtd, FL_READING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1494 | this->select_chip(mtd, chip); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | /* Add requested oob length */ |
| 1497 | len += ooblen; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | while (len) { |
| 1500 | if (sndcmd) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1501 | this->cmdfunc(mtd, NAND_CMD_READ0, 0, page & this->pagemask); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1502 | sndcmd = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1504 | this->read_buf(mtd, &buf[cnt], pagesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | |
| 1506 | len -= pagesize; |
| 1507 | cnt += pagesize; |
| 1508 | page++; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1509 | |
| 1510 | if (!this->dev_ready) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1511 | udelay(this->chip_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | else |
Thomas Gleixner | 3b88775 | 2005-02-22 21:56:49 +0000 | [diff] [blame] | 1513 | nand_wait_ready(mtd); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1514 | |
| 1515 | /* Check, if the chip supports auto page increment */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | if (!NAND_CANAUTOINCR(this) || !(page & blockcheck)) |
| 1517 | sndcmd = 1; |
| 1518 | } |
| 1519 | |
| 1520 | /* Deselect and wake up anyone waiting on the device */ |
| 1521 | nand_release_device(mtd); |
| 1522 | return 0; |
| 1523 | } |
| 1524 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1525 | /** |
| 1526 | * nand_prepare_oobbuf - [GENERIC] Prepare the out of band buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | * @mtd: MTD device structure |
| 1528 | * @fsbuf: buffer given by fs driver |
| 1529 | * @oobsel: out of band selection structre |
| 1530 | * @autoplace: 1 = place given buffer into the oob bytes |
| 1531 | * @numpages: number of pages to prepare |
| 1532 | * |
| 1533 | * Return: |
| 1534 | * 1. Filesystem buffer available and autoplacement is off, |
| 1535 | * return filesystem buffer |
| 1536 | * 2. No filesystem buffer or autoplace is off, return internal |
| 1537 | * buffer |
| 1538 | * 3. Filesystem buffer is given and autoplace selected |
| 1539 | * put data from fs buffer into internal buffer and |
| 1540 | * retrun internal buffer |
| 1541 | * |
| 1542 | * Note: The internal buffer is filled with 0xff. This must |
| 1543 | * be done only once, when no autoplacement happens |
| 1544 | * Autoplacement sets the buffer dirty flag, which |
| 1545 | * forces the 0xff fill before using the buffer again. |
| 1546 | * |
| 1547 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1548 | static u_char *nand_prepare_oobbuf(struct mtd_info *mtd, u_char *fsbuf, struct nand_oobinfo *oobsel, |
| 1549 | int autoplace, int numpages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | { |
| 1551 | struct nand_chip *this = mtd->priv; |
| 1552 | int i, len, ofs; |
| 1553 | |
| 1554 | /* Zero copy fs supplied buffer */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1555 | if (fsbuf && !autoplace) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | return fsbuf; |
| 1557 | |
| 1558 | /* Check, if the buffer must be filled with ff again */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1559 | if (this->oobdirty) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1560 | memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | this->oobdirty = 0; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | /* If we have no autoplacement or no fs buffer use the internal one */ |
| 1565 | if (!autoplace || !fsbuf) |
| 1566 | return this->oob_buf; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | /* Walk through the pages and place the data */ |
| 1569 | this->oobdirty = 1; |
| 1570 | ofs = 0; |
| 1571 | while (numpages--) { |
| 1572 | for (i = 0, len = 0; len < mtd->oobavail; i++) { |
| 1573 | int to = ofs + oobsel->oobfree[i][0]; |
| 1574 | int num = oobsel->oobfree[i][1]; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1575 | memcpy(&this->oob_buf[to], fsbuf, num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | len += num; |
| 1577 | fsbuf += num; |
| 1578 | } |
| 1579 | ofs += mtd->oobavail; |
| 1580 | } |
| 1581 | return this->oob_buf; |
| 1582 | } |
| 1583 | |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 1584 | #define NOTALIGNED(x) (x & (mtd->writesize-1)) != 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | |
| 1586 | /** |
| 1587 | * nand_write - [MTD Interface] compability function for nand_write_ecc |
| 1588 | * @mtd: MTD device structure |
| 1589 | * @to: offset to write to |
| 1590 | * @len: number of bytes to write |
| 1591 | * @retlen: pointer to variable to store the number of written bytes |
| 1592 | * @buf: the data to write |
| 1593 | * |
| 1594 | * This function simply calls nand_write_ecc with oob buffer and oobsel = NULL |
| 1595 | * |
| 1596 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1597 | static int nand_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1599 | return (nand_write_ecc(mtd, to, len, retlen, buf, NULL, NULL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | /** |
| 1603 | * nand_write_ecc - [MTD Interface] NAND write with ECC |
| 1604 | * @mtd: MTD device structure |
| 1605 | * @to: offset to write to |
| 1606 | * @len: number of bytes to write |
| 1607 | * @retlen: pointer to variable to store the number of written bytes |
| 1608 | * @buf: the data to write |
| 1609 | * @eccbuf: filesystem supplied oob data buffer |
| 1610 | * @oobsel: oob selection structure |
| 1611 | * |
| 1612 | * NAND write with ECC |
| 1613 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1614 | static int nand_write_ecc(struct mtd_info *mtd, loff_t to, size_t len, |
| 1615 | size_t *retlen, const u_char *buf, u_char *eccbuf, |
| 1616 | struct nand_oobinfo *oobsel) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | { |
| 1618 | int startpage, page, ret = -EIO, oob = 0, written = 0, chipnr; |
| 1619 | int autoplace = 0, numpages, totalpages; |
| 1620 | struct nand_chip *this = mtd->priv; |
| 1621 | u_char *oobbuf, *bufstart; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1622 | int ppblock = (1 << (this->phys_erase_shift - this->page_shift)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1624 | DEBUG(MTD_DEBUG_LEVEL3, "nand_write_ecc: to = 0x%08x, len = %i\n", (unsigned int)to, (int)len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | |
| 1626 | /* Initialize retlen, in case of early exit */ |
| 1627 | *retlen = 0; |
| 1628 | |
| 1629 | /* Do not allow write past end of device */ |
| 1630 | if ((to + len) > mtd->size) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1631 | DEBUG(MTD_DEBUG_LEVEL0, "nand_write_ecc: Attempt to write past end of page\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | return -EINVAL; |
| 1633 | } |
| 1634 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1635 | /* reject writes, which are not page aligned */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1636 | if (NOTALIGNED(to) || NOTALIGNED(len)) { |
| 1637 | printk(KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | return -EINVAL; |
| 1639 | } |
| 1640 | |
| 1641 | /* Grab the lock and see if the device is available */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1642 | nand_get_device(this, mtd, FL_WRITING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | |
| 1644 | /* Calculate chipnr */ |
| 1645 | chipnr = (int)(to >> this->chip_shift); |
| 1646 | /* Select the NAND device */ |
| 1647 | this->select_chip(mtd, chipnr); |
| 1648 | |
| 1649 | /* Check, if it is write protected */ |
| 1650 | if (nand_check_wp(mtd)) |
| 1651 | goto out; |
| 1652 | |
| 1653 | /* if oobsel is NULL, use chip defaults */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1654 | if (oobsel == NULL) |
| 1655 | oobsel = &mtd->oobinfo; |
| 1656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | /* Autoplace of oob data ? Use the default placement scheme */ |
| 1658 | if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) { |
| 1659 | oobsel = this->autooob; |
| 1660 | autoplace = 1; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1661 | } |
Thomas Gleixner | 90e260c | 2005-05-19 17:10:26 +0100 | [diff] [blame] | 1662 | if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR) |
| 1663 | autoplace = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | |
| 1665 | /* Setup variables and oob buffer */ |
| 1666 | totalpages = len >> this->page_shift; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1667 | page = (int)(to >> this->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | /* Invalidate the page cache, if we write to the cached page */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1669 | if (page <= this->pagebuf && this->pagebuf < (page + totalpages)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | this->pagebuf = -1; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | /* Set it relative to chip */ |
| 1673 | page &= this->pagemask; |
| 1674 | startpage = page; |
| 1675 | /* Calc number of pages we can write in one go */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1676 | numpages = min(ppblock - (startpage & (ppblock - 1)), totalpages); |
| 1677 | oobbuf = nand_prepare_oobbuf(mtd, eccbuf, oobsel, autoplace, numpages); |
| 1678 | bufstart = (u_char *) buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | |
| 1680 | /* Loop until all data is written */ |
| 1681 | while (written < len) { |
| 1682 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1683 | this->data_poi = (u_char *) &buf[written]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | /* Write one page. If this is the last page to write |
| 1685 | * or the last page in this block, then use the |
| 1686 | * real pageprogram command, else select cached programming |
| 1687 | * if supported by the chip. |
| 1688 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1689 | ret = nand_write_page(mtd, this, page, &oobbuf[oob], oobsel, (--numpages > 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | if (ret) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1691 | DEBUG(MTD_DEBUG_LEVEL0, "nand_write_ecc: write_page failed %d\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | goto out; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1693 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | /* Next oob page */ |
| 1695 | oob += mtd->oobsize; |
| 1696 | /* Update written bytes count */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 1697 | written += mtd->writesize; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1698 | if (written == len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | goto cmp; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | /* Increment page address */ |
| 1702 | page++; |
| 1703 | |
| 1704 | /* Have we hit a block boundary ? Then we have to verify and |
| 1705 | * if verify is ok, we have to setup the oob buffer for |
| 1706 | * the next pages. |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1707 | */ |
| 1708 | if (!(page & (ppblock - 1))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | int ofs; |
| 1710 | this->data_poi = bufstart; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1711 | ret = nand_verify_pages(mtd, this, startpage, page - startpage, |
| 1712 | oobbuf, oobsel, chipnr, (eccbuf != NULL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | if (ret) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1714 | DEBUG(MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | goto out; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1716 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | *retlen = written; |
| 1718 | |
| 1719 | ofs = autoplace ? mtd->oobavail : mtd->oobsize; |
| 1720 | if (eccbuf) |
| 1721 | eccbuf += (page - startpage) * ofs; |
| 1722 | totalpages -= page - startpage; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1723 | numpages = min(totalpages, ppblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | page &= this->pagemask; |
| 1725 | startpage = page; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1726 | oobbuf = nand_prepare_oobbuf(mtd, eccbuf, oobsel, autoplace, numpages); |
Todd Poynor | 868801e | 2005-11-05 03:21:15 +0000 | [diff] [blame] | 1727 | oob = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | /* Check, if we cross a chip boundary */ |
| 1729 | if (!page) { |
| 1730 | chipnr++; |
| 1731 | this->select_chip(mtd, -1); |
| 1732 | this->select_chip(mtd, chipnr); |
| 1733 | } |
| 1734 | } |
| 1735 | } |
| 1736 | /* Verify the remaining pages */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1737 | cmp: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | this->data_poi = bufstart; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1739 | ret = nand_verify_pages(mtd, this, startpage, totalpages, oobbuf, oobsel, chipnr, (eccbuf != NULL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | if (!ret) |
| 1741 | *retlen = written; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1742 | else |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1743 | DEBUG(MTD_DEBUG_LEVEL0, "nand_write_ecc: verify_pages failed %d\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1745 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | /* Deselect and wake up anyone waiting on the device */ |
| 1747 | nand_release_device(mtd); |
| 1748 | |
| 1749 | return ret; |
| 1750 | } |
| 1751 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | /** |
| 1753 | * nand_write_oob - [MTD Interface] NAND write out-of-band |
| 1754 | * @mtd: MTD device structure |
| 1755 | * @to: offset to write to |
| 1756 | * @len: number of bytes to write |
| 1757 | * @retlen: pointer to variable to store the number of written bytes |
| 1758 | * @buf: the data to write |
| 1759 | * |
| 1760 | * NAND write out-of-band |
| 1761 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1762 | static int nand_write_oob(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | { |
| 1764 | int column, page, status, ret = -EIO, chipnr; |
| 1765 | struct nand_chip *this = mtd->priv; |
| 1766 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1767 | DEBUG(MTD_DEBUG_LEVEL3, "nand_write_oob: to = 0x%08x, len = %i\n", (unsigned int)to, (int)len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | |
| 1769 | /* Shift to get page */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1770 | page = (int)(to >> this->page_shift); |
| 1771 | chipnr = (int)(to >> this->chip_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | |
| 1773 | /* Mask to get column */ |
| 1774 | column = to & (mtd->oobsize - 1); |
| 1775 | |
| 1776 | /* Initialize return length value */ |
| 1777 | *retlen = 0; |
| 1778 | |
| 1779 | /* Do not allow write past end of page */ |
| 1780 | if ((column + len) > mtd->oobsize) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1781 | DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: Attempt to write past end of page\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | return -EINVAL; |
| 1783 | } |
| 1784 | |
| 1785 | /* Grab the lock and see if the device is available */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1786 | nand_get_device(this, mtd, FL_WRITING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | |
| 1788 | /* Select the NAND device */ |
| 1789 | this->select_chip(mtd, chipnr); |
| 1790 | |
| 1791 | /* Reset the chip. Some chips (like the Toshiba TC5832DC found |
| 1792 | in one of my DiskOnChip 2000 test units) will clear the whole |
| 1793 | data page too if we don't do this. I have no clue why, but |
| 1794 | I seem to have 'fixed' it in the doc2000 driver in |
| 1795 | August 1999. dwmw2. */ |
| 1796 | this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
| 1797 | |
| 1798 | /* Check, if it is write protected */ |
| 1799 | if (nand_check_wp(mtd)) |
| 1800 | goto out; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1801 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | /* Invalidate the page cache, if we write to the cached page */ |
| 1803 | if (page == this->pagebuf) |
| 1804 | this->pagebuf = -1; |
| 1805 | |
| 1806 | if (NAND_MUST_PAD(this)) { |
| 1807 | /* Write out desired data */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 1808 | this->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page & this->pagemask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | /* prepad 0xff for partial programming */ |
| 1810 | this->write_buf(mtd, ffchars, column); |
| 1811 | /* write data */ |
| 1812 | this->write_buf(mtd, buf, len); |
| 1813 | /* postpad 0xff for partial programming */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1814 | this->write_buf(mtd, ffchars, mtd->oobsize - (len + column)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | } else { |
| 1816 | /* Write out desired data */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 1817 | this->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize + column, page & this->pagemask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | /* write data */ |
| 1819 | this->write_buf(mtd, buf, len); |
| 1820 | } |
| 1821 | /* Send command to program the OOB data */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1822 | this->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1824 | status = this->waitfunc(mtd, this, FL_WRITING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | |
| 1826 | /* See if device thinks it succeeded */ |
David A. Marlin | a4ab4c5 | 2005-01-23 18:30:53 +0000 | [diff] [blame] | 1827 | if (status & NAND_STATUS_FAIL) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1828 | DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write, page 0x%08x\n", page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | ret = -EIO; |
| 1830 | goto out; |
| 1831 | } |
| 1832 | /* Return happy */ |
| 1833 | *retlen = len; |
| 1834 | |
| 1835 | #ifdef CONFIG_MTD_NAND_VERIFY_WRITE |
| 1836 | /* Send command to read back the data */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1837 | this->cmdfunc(mtd, NAND_CMD_READOOB, column, page & this->pagemask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | |
| 1839 | if (this->verify_buf(mtd, buf, len)) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1840 | DEBUG(MTD_DEBUG_LEVEL0, "nand_write_oob: " "Failed write verify, page 0x%08x\n", page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | ret = -EIO; |
| 1842 | goto out; |
| 1843 | } |
| 1844 | #endif |
| 1845 | ret = 0; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1846 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | /* Deselect and wake up anyone waiting on the device */ |
| 1848 | nand_release_device(mtd); |
| 1849 | |
| 1850 | return ret; |
| 1851 | } |
| 1852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | /** |
| 1854 | * nand_writev - [MTD Interface] compabilty function for nand_writev_ecc |
| 1855 | * @mtd: MTD device structure |
| 1856 | * @vecs: the iovectors to write |
| 1857 | * @count: number of vectors |
| 1858 | * @to: offset to write to |
| 1859 | * @retlen: pointer to variable to store the number of written bytes |
| 1860 | * |
| 1861 | * NAND write with kvec. This just calls the ecc function |
| 1862 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1863 | static int nand_writev(struct mtd_info *mtd, const struct kvec *vecs, unsigned long count, |
| 1864 | loff_t to, size_t *retlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1866 | return (nand_writev_ecc(mtd, vecs, count, to, retlen, NULL, NULL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | /** |
| 1870 | * nand_writev_ecc - [MTD Interface] write with iovec with ecc |
| 1871 | * @mtd: MTD device structure |
| 1872 | * @vecs: the iovectors to write |
| 1873 | * @count: number of vectors |
| 1874 | * @to: offset to write to |
| 1875 | * @retlen: pointer to variable to store the number of written bytes |
| 1876 | * @eccbuf: filesystem supplied oob data buffer |
| 1877 | * @oobsel: oob selection structure |
| 1878 | * |
| 1879 | * NAND write with iovec with ecc |
| 1880 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1881 | static int nand_writev_ecc(struct mtd_info *mtd, const struct kvec *vecs, unsigned long count, |
| 1882 | loff_t to, size_t *retlen, u_char *eccbuf, struct nand_oobinfo *oobsel) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | { |
| 1884 | int i, page, len, total_len, ret = -EIO, written = 0, chipnr; |
| 1885 | int oob, numpages, autoplace = 0, startpage; |
| 1886 | struct nand_chip *this = mtd->priv; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1887 | int ppblock = (1 << (this->phys_erase_shift - this->page_shift)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | u_char *oobbuf, *bufstart; |
| 1889 | |
| 1890 | /* Preset written len for early exit */ |
| 1891 | *retlen = 0; |
| 1892 | |
| 1893 | /* Calculate total length of data */ |
| 1894 | total_len = 0; |
| 1895 | for (i = 0; i < count; i++) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1896 | total_len += (int)vecs[i].iov_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1898 | DEBUG(MTD_DEBUG_LEVEL3, "nand_writev: to = 0x%08x, len = %i, count = %ld\n", (unsigned int)to, (unsigned int)total_len, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | |
| 1900 | /* Do not allow write past end of page */ |
| 1901 | if ((to + total_len) > mtd->size) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1902 | DEBUG(MTD_DEBUG_LEVEL0, "nand_writev: Attempted write past end of device\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | return -EINVAL; |
| 1904 | } |
| 1905 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1906 | /* reject writes, which are not page aligned */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1907 | if (NOTALIGNED(to) || NOTALIGNED(total_len)) { |
| 1908 | printk(KERN_NOTICE "nand_write_ecc: Attempt to write not page aligned data\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | return -EINVAL; |
| 1910 | } |
| 1911 | |
| 1912 | /* Grab the lock and see if the device is available */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1913 | nand_get_device(this, mtd, FL_WRITING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | |
| 1915 | /* Get the current chip-nr */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1916 | chipnr = (int)(to >> this->chip_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | /* Select the NAND device */ |
| 1918 | this->select_chip(mtd, chipnr); |
| 1919 | |
| 1920 | /* Check, if it is write protected */ |
| 1921 | if (nand_check_wp(mtd)) |
| 1922 | goto out; |
| 1923 | |
| 1924 | /* if oobsel is NULL, use chip defaults */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1925 | if (oobsel == NULL) |
| 1926 | oobsel = &mtd->oobinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | |
| 1928 | /* Autoplace of oob data ? Use the default placement scheme */ |
| 1929 | if (oobsel->useecc == MTD_NANDECC_AUTOPLACE) { |
| 1930 | oobsel = this->autooob; |
| 1931 | autoplace = 1; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1932 | } |
Thomas Gleixner | 90e260c | 2005-05-19 17:10:26 +0100 | [diff] [blame] | 1933 | if (oobsel->useecc == MTD_NANDECC_AUTOPL_USR) |
| 1934 | autoplace = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | |
| 1936 | /* Setup start page */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1937 | page = (int)(to >> this->page_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | /* Invalidate the page cache, if we write to the cached page */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1939 | if (page <= this->pagebuf && this->pagebuf < ((to + total_len) >> this->page_shift)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | this->pagebuf = -1; |
| 1941 | |
| 1942 | startpage = page & this->pagemask; |
| 1943 | |
| 1944 | /* Loop until all kvec' data has been written */ |
| 1945 | len = 0; |
| 1946 | while (count) { |
| 1947 | /* If the given tuple is >= pagesize then |
| 1948 | * write it out from the iov |
| 1949 | */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 1950 | if ((vecs->iov_len - len) >= mtd->writesize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | /* Calc number of pages we can write |
| 1952 | * out of this iov in one go */ |
| 1953 | numpages = (vecs->iov_len - len) >> this->page_shift; |
| 1954 | /* Do not cross block boundaries */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1955 | numpages = min(ppblock - (startpage & (ppblock - 1)), numpages); |
| 1956 | oobbuf = nand_prepare_oobbuf(mtd, NULL, oobsel, autoplace, numpages); |
| 1957 | bufstart = (u_char *) vecs->iov_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | bufstart += len; |
| 1959 | this->data_poi = bufstart; |
| 1960 | oob = 0; |
| 1961 | for (i = 1; i <= numpages; i++) { |
| 1962 | /* Write one page. If this is the last page to write |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1963 | * then use the real pageprogram command, else select |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | * cached programming if supported by the chip. |
| 1965 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1966 | ret = nand_write_page(mtd, this, page & this->pagemask, |
| 1967 | &oobbuf[oob], oobsel, i != numpages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | if (ret) |
| 1969 | goto out; |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 1970 | this->data_poi += mtd->writesize; |
| 1971 | len += mtd->writesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | oob += mtd->oobsize; |
| 1973 | page++; |
| 1974 | } |
| 1975 | /* Check, if we have to switch to the next tuple */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1976 | if (len >= (int)vecs->iov_len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | vecs++; |
| 1978 | len = 0; |
| 1979 | count--; |
| 1980 | } |
| 1981 | } else { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1982 | /* We must use the internal buffer, read data out of each |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | * tuple until we have a full page to write |
| 1984 | */ |
| 1985 | int cnt = 0; |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 1986 | while (cnt < mtd->writesize) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1987 | if (vecs->iov_base != NULL && vecs->iov_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | this->data_buf[cnt++] = ((u_char *) vecs->iov_base)[len++]; |
| 1989 | /* Check, if we have to switch to the next tuple */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 1990 | if (len >= (int)vecs->iov_len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | vecs++; |
| 1992 | len = 0; |
| 1993 | count--; |
| 1994 | } |
| 1995 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1996 | this->pagebuf = page; |
| 1997 | this->data_poi = this->data_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | bufstart = this->data_poi; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1999 | numpages = 1; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2000 | oobbuf = nand_prepare_oobbuf(mtd, NULL, oobsel, autoplace, numpages); |
| 2001 | ret = nand_write_page(mtd, this, page & this->pagemask, oobbuf, oobsel, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | if (ret) |
| 2003 | goto out; |
| 2004 | page++; |
| 2005 | } |
| 2006 | |
| 2007 | this->data_poi = bufstart; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2008 | ret = nand_verify_pages(mtd, this, startpage, numpages, oobbuf, oobsel, chipnr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | if (ret) |
| 2010 | goto out; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2011 | |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 2012 | written += mtd->writesize * numpages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | /* All done ? */ |
| 2014 | if (!count) |
| 2015 | break; |
| 2016 | |
| 2017 | startpage = page & this->pagemask; |
| 2018 | /* Check, if we cross a chip boundary */ |
| 2019 | if (!startpage) { |
| 2020 | chipnr++; |
| 2021 | this->select_chip(mtd, -1); |
| 2022 | this->select_chip(mtd, chipnr); |
| 2023 | } |
| 2024 | } |
| 2025 | ret = 0; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2026 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | /* Deselect and wake up anyone waiting on the device */ |
| 2028 | nand_release_device(mtd); |
| 2029 | |
| 2030 | *retlen = written; |
| 2031 | return ret; |
| 2032 | } |
| 2033 | |
| 2034 | /** |
| 2035 | * single_erease_cmd - [GENERIC] NAND standard block erase command function |
| 2036 | * @mtd: MTD device structure |
| 2037 | * @page: the page address of the block which will be erased |
| 2038 | * |
| 2039 | * Standard erase command for NAND chips |
| 2040 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2041 | static void single_erase_cmd(struct mtd_info *mtd, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | { |
| 2043 | struct nand_chip *this = mtd->priv; |
| 2044 | /* Send commands to erase a block */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2045 | this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
| 2046 | this->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | } |
| 2048 | |
| 2049 | /** |
| 2050 | * multi_erease_cmd - [GENERIC] AND specific block erase command function |
| 2051 | * @mtd: MTD device structure |
| 2052 | * @page: the page address of the block which will be erased |
| 2053 | * |
| 2054 | * AND multi block erase command function |
| 2055 | * Erase 4 consecutive blocks |
| 2056 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2057 | static void multi_erase_cmd(struct mtd_info *mtd, int page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | { |
| 2059 | struct nand_chip *this = mtd->priv; |
| 2060 | /* Send commands to erase a block */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2061 | this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++); |
| 2062 | this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++); |
| 2063 | this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page++); |
| 2064 | this->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page); |
| 2065 | this->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | /** |
| 2069 | * nand_erase - [MTD Interface] erase block(s) |
| 2070 | * @mtd: MTD device structure |
| 2071 | * @instr: erase instruction |
| 2072 | * |
| 2073 | * Erase one ore more blocks |
| 2074 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2075 | static int nand_erase(struct mtd_info *mtd, struct erase_info *instr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2077 | return nand_erase_nand(mtd, instr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2079 | |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 2080 | #define BBT_PAGE_MASK 0xffffff3f |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | /** |
| 2082 | * nand_erase_intern - [NAND Interface] erase block(s) |
| 2083 | * @mtd: MTD device structure |
| 2084 | * @instr: erase instruction |
| 2085 | * @allowbbt: allow erasing the bbt area |
| 2086 | * |
| 2087 | * Erase one ore more blocks |
| 2088 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2089 | int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, int allowbbt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | { |
| 2091 | int page, len, status, pages_per_block, ret, chipnr; |
| 2092 | struct nand_chip *this = mtd->priv; |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 2093 | int rewrite_bbt[NAND_MAX_CHIPS]={0}; /* flags to indicate the page, if bbt needs to be rewritten. */ |
| 2094 | unsigned int bbt_masked_page; /* bbt mask to compare to page being erased. */ |
| 2095 | /* It is used to see if the current page is in the same */ |
| 2096 | /* 256 block group and the same bank as the bbt. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2098 | DEBUG(MTD_DEBUG_LEVEL3, "nand_erase: start = 0x%08x, len = %i\n", (unsigned int)instr->addr, (unsigned int)instr->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | |
| 2100 | /* Start address must align on block boundary */ |
| 2101 | if (instr->addr & ((1 << this->phys_erase_shift) - 1)) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2102 | DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Unaligned address\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | return -EINVAL; |
| 2104 | } |
| 2105 | |
| 2106 | /* Length must align on block boundary */ |
| 2107 | if (instr->len & ((1 << this->phys_erase_shift) - 1)) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2108 | DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Length not block aligned\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | return -EINVAL; |
| 2110 | } |
| 2111 | |
| 2112 | /* Do not allow erase past end of device */ |
| 2113 | if ((instr->len + instr->addr) > mtd->size) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2114 | DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Erase past end of device\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | return -EINVAL; |
| 2116 | } |
| 2117 | |
| 2118 | instr->fail_addr = 0xffffffff; |
| 2119 | |
| 2120 | /* Grab the lock and see if the device is available */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2121 | nand_get_device(this, mtd, FL_ERASING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | |
| 2123 | /* Shift to get first page */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2124 | page = (int)(instr->addr >> this->page_shift); |
| 2125 | chipnr = (int)(instr->addr >> this->chip_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | |
| 2127 | /* Calculate pages in each block */ |
| 2128 | pages_per_block = 1 << (this->phys_erase_shift - this->page_shift); |
| 2129 | |
| 2130 | /* Select the NAND device */ |
| 2131 | this->select_chip(mtd, chipnr); |
| 2132 | |
| 2133 | /* Check the WP bit */ |
| 2134 | /* Check, if it is write protected */ |
| 2135 | if (nand_check_wp(mtd)) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2136 | DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: Device is write protected!!!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | instr->state = MTD_ERASE_FAILED; |
| 2138 | goto erase_exit; |
| 2139 | } |
| 2140 | |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 2141 | /* if BBT requires refresh, set the BBT page mask to see if the BBT should be rewritten */ |
| 2142 | if (this->options & BBT_AUTO_REFRESH) { |
| 2143 | bbt_masked_page = this->bbt_td->pages[chipnr] & BBT_PAGE_MASK; |
| 2144 | } else { |
| 2145 | bbt_masked_page = 0xffffffff; /* should not match anything */ |
| 2146 | } |
| 2147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | /* Loop through the pages */ |
| 2149 | len = instr->len; |
| 2150 | |
| 2151 | instr->state = MTD_ERASING; |
| 2152 | |
| 2153 | while (len) { |
| 2154 | /* Check if we have a bad block, we do not erase bad blocks ! */ |
| 2155 | if (nand_block_checkbad(mtd, ((loff_t) page) << this->page_shift, 0, allowbbt)) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2156 | printk(KERN_WARNING "nand_erase: attempt to erase a bad block at page 0x%08x\n", page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | instr->state = MTD_ERASE_FAILED; |
| 2158 | goto erase_exit; |
| 2159 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2160 | |
| 2161 | /* Invalidate the page cache, if we erase the block which contains |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | the current cached page */ |
| 2163 | if (page <= this->pagebuf && this->pagebuf < (page + pages_per_block)) |
| 2164 | this->pagebuf = -1; |
| 2165 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2166 | this->erase_cmd(mtd, page & this->pagemask); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2167 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2168 | status = this->waitfunc(mtd, this, FL_ERASING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | |
David A. Marlin | 068e3c0 | 2005-01-24 03:07:46 +0000 | [diff] [blame] | 2170 | /* See if operation failed and additional status checks are available */ |
| 2171 | if ((status & NAND_STATUS_FAIL) && (this->errstat)) { |
| 2172 | status = this->errstat(mtd, this, FL_ERASING, status, page); |
| 2173 | } |
| 2174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | /* See if block erase succeeded */ |
David A. Marlin | a4ab4c5 | 2005-01-23 18:30:53 +0000 | [diff] [blame] | 2176 | if (status & NAND_STATUS_FAIL) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2177 | DEBUG(MTD_DEBUG_LEVEL0, "nand_erase: " "Failed erase, page 0x%08x\n", page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | instr->state = MTD_ERASE_FAILED; |
| 2179 | instr->fail_addr = (page << this->page_shift); |
| 2180 | goto erase_exit; |
| 2181 | } |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 2182 | |
| 2183 | /* if BBT requires refresh, set the BBT rewrite flag to the page being erased */ |
| 2184 | if (this->options & BBT_AUTO_REFRESH) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2185 | if (((page & BBT_PAGE_MASK) == bbt_masked_page) && |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 2186 | (page != this->bbt_td->pages[chipnr])) { |
| 2187 | rewrite_bbt[chipnr] = (page << this->page_shift); |
| 2188 | } |
| 2189 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | /* Increment page address and decrement length */ |
| 2192 | len -= (1 << this->phys_erase_shift); |
| 2193 | page += pages_per_block; |
| 2194 | |
| 2195 | /* Check, if we cross a chip boundary */ |
| 2196 | if (len && !(page & this->pagemask)) { |
| 2197 | chipnr++; |
| 2198 | this->select_chip(mtd, -1); |
| 2199 | this->select_chip(mtd, chipnr); |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 2200 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2201 | /* if BBT requires refresh and BBT-PERCHIP, |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 2202 | * set the BBT page mask to see if this BBT should be rewritten */ |
| 2203 | if ((this->options & BBT_AUTO_REFRESH) && (this->bbt_td->options & NAND_BBT_PERCHIP)) { |
| 2204 | bbt_masked_page = this->bbt_td->pages[chipnr] & BBT_PAGE_MASK; |
| 2205 | } |
| 2206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | } |
| 2208 | } |
| 2209 | instr->state = MTD_ERASE_DONE; |
| 2210 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2211 | erase_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | |
| 2213 | ret = instr->state == MTD_ERASE_DONE ? 0 : -EIO; |
| 2214 | /* Do call back function */ |
| 2215 | if (!ret) |
| 2216 | mtd_erase_callback(instr); |
| 2217 | |
| 2218 | /* Deselect and wake up anyone waiting on the device */ |
| 2219 | nand_release_device(mtd); |
| 2220 | |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 2221 | /* if BBT requires refresh and erase was successful, rewrite any selected bad block tables */ |
| 2222 | if ((this->options & BBT_AUTO_REFRESH) && (!ret)) { |
| 2223 | for (chipnr = 0; chipnr < this->numchips; chipnr++) { |
| 2224 | if (rewrite_bbt[chipnr]) { |
| 2225 | /* update the BBT for chip */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2226 | DEBUG(MTD_DEBUG_LEVEL0, "nand_erase_nand: nand_update_bbt (%d:0x%0x 0x%0x)\n", |
| 2227 | chipnr, rewrite_bbt[chipnr], this->bbt_td->pages[chipnr]); |
| 2228 | nand_update_bbt(mtd, rewrite_bbt[chipnr]); |
David A. Marlin | 30f464b | 2005-01-17 18:35:25 +0000 | [diff] [blame] | 2229 | } |
| 2230 | } |
| 2231 | } |
| 2232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | /* Return more or less happy */ |
| 2234 | return ret; |
| 2235 | } |
| 2236 | |
| 2237 | /** |
| 2238 | * nand_sync - [MTD Interface] sync |
| 2239 | * @mtd: MTD device structure |
| 2240 | * |
| 2241 | * Sync is actually a wait for chip ready function |
| 2242 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2243 | static void nand_sync(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | { |
| 2245 | struct nand_chip *this = mtd->priv; |
| 2246 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2247 | DEBUG(MTD_DEBUG_LEVEL3, "nand_sync: called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | |
| 2249 | /* Grab the lock and see if the device is available */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2250 | nand_get_device(this, mtd, FL_SYNCING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | /* Release it and go back */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2252 | nand_release_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | } |
| 2254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | /** |
| 2256 | * nand_block_isbad - [MTD Interface] Check whether the block at the given offset is bad |
| 2257 | * @mtd: MTD device structure |
| 2258 | * @ofs: offset relative to mtd start |
| 2259 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2260 | static int nand_block_isbad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2261 | { |
| 2262 | /* Check for invalid offset */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2263 | if (ofs > mtd->size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | return -EINVAL; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2265 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2266 | return nand_block_checkbad(mtd, ofs, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | /** |
| 2270 | * nand_block_markbad - [MTD Interface] Mark the block at the given offset as bad |
| 2271 | * @mtd: MTD device structure |
| 2272 | * @ofs: offset relative to mtd start |
| 2273 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2274 | static int nand_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | { |
| 2276 | struct nand_chip *this = mtd->priv; |
| 2277 | int ret; |
| 2278 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2279 | if ((ret = nand_block_isbad(mtd, ofs))) { |
| 2280 | /* If it was bad already, return success and do nothing. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | if (ret > 0) |
| 2282 | return 0; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2283 | return ret; |
| 2284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | |
| 2286 | return this->block_markbad(mtd, ofs); |
| 2287 | } |
| 2288 | |
| 2289 | /** |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 2290 | * nand_suspend - [MTD Interface] Suspend the NAND flash |
| 2291 | * @mtd: MTD device structure |
| 2292 | */ |
| 2293 | static int nand_suspend(struct mtd_info *mtd) |
| 2294 | { |
| 2295 | struct nand_chip *this = mtd->priv; |
| 2296 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2297 | return nand_get_device(this, mtd, FL_PM_SUSPENDED); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | /** |
| 2301 | * nand_resume - [MTD Interface] Resume the NAND flash |
| 2302 | * @mtd: MTD device structure |
| 2303 | */ |
| 2304 | static void nand_resume(struct mtd_info *mtd) |
| 2305 | { |
| 2306 | struct nand_chip *this = mtd->priv; |
| 2307 | |
| 2308 | if (this->state == FL_PM_SUSPENDED) |
| 2309 | nand_release_device(mtd); |
| 2310 | else |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2311 | printk(KERN_ERR "resume() called for the chip which is not in suspended state\n"); |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 2312 | |
| 2313 | } |
| 2314 | |
David Woodhouse | 52239da | 2006-05-14 16:54:39 +0100 | [diff] [blame] | 2315 | /* module_text_address() isn't exported, and it's mostly a pointless |
| 2316 | test if this is a module _anyway_ -- they'd have to try _really_ hard |
| 2317 | to call us from in-kernel code if the core NAND support is modular. */ |
| 2318 | #ifdef MODULE |
| 2319 | #define caller_is_module() (1) |
| 2320 | #else |
| 2321 | #define caller_is_module() module_text_address((unsigned long)__builtin_return_address(0)) |
| 2322 | #endif |
| 2323 | |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 2324 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2325 | * nand_scan - [NAND Interface] Scan for the NAND device |
| 2326 | * @mtd: MTD device structure |
| 2327 | * @maxchips: Number of chips to scan for |
| 2328 | * |
David Woodhouse | 552d920 | 2006-05-14 01:20:46 +0100 | [diff] [blame] | 2329 | * This fills out all the uninitialized function pointers |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | * with the defaults. |
| 2331 | * The flash ID is read and the mtd/chip structures are |
| 2332 | * filled with the appropriate values. Buffers are allocated if |
| 2333 | * they are not provided by the board driver |
David Woodhouse | 552d920 | 2006-05-14 01:20:46 +0100 | [diff] [blame] | 2334 | * The mtd->owner field must be set to the module of the caller |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | * |
| 2336 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2337 | int nand_scan(struct mtd_info *mtd, int maxchips) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | { |
Ben Dooks | 3b946e3 | 2005-03-14 18:30:48 +0000 | [diff] [blame] | 2339 | int i, nand_maf_id, nand_dev_id, busw, maf_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2340 | struct nand_chip *this = mtd->priv; |
| 2341 | |
David Woodhouse | 52239da | 2006-05-14 16:54:39 +0100 | [diff] [blame] | 2342 | /* Many callers got this wrong, so check for it for a while... */ |
| 2343 | if (!mtd->owner && caller_is_module()) { |
David Woodhouse | 552d920 | 2006-05-14 01:20:46 +0100 | [diff] [blame] | 2344 | printk(KERN_CRIT "nand_scan() called with NULL mtd->owner!\n"); |
| 2345 | BUG(); |
| 2346 | } |
| 2347 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2348 | /* Get buswidth to select the correct functions */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2349 | busw = this->options & NAND_BUSWIDTH_16; |
| 2350 | |
| 2351 | /* check for proper chip_delay setup, set 20us if not */ |
| 2352 | if (!this->chip_delay) |
| 2353 | this->chip_delay = 20; |
| 2354 | |
| 2355 | /* check, if a user supplied command function given */ |
| 2356 | if (this->cmdfunc == NULL) |
| 2357 | this->cmdfunc = nand_command; |
| 2358 | |
| 2359 | /* check, if a user supplied wait function given */ |
| 2360 | if (this->waitfunc == NULL) |
| 2361 | this->waitfunc = nand_wait; |
| 2362 | |
| 2363 | if (!this->select_chip) |
| 2364 | this->select_chip = nand_select_chip; |
| 2365 | if (!this->write_byte) |
| 2366 | this->write_byte = busw ? nand_write_byte16 : nand_write_byte; |
| 2367 | if (!this->read_byte) |
| 2368 | this->read_byte = busw ? nand_read_byte16 : nand_read_byte; |
| 2369 | if (!this->write_word) |
| 2370 | this->write_word = nand_write_word; |
| 2371 | if (!this->read_word) |
| 2372 | this->read_word = nand_read_word; |
| 2373 | if (!this->block_bad) |
| 2374 | this->block_bad = nand_block_bad; |
| 2375 | if (!this->block_markbad) |
| 2376 | this->block_markbad = nand_default_block_markbad; |
| 2377 | if (!this->write_buf) |
| 2378 | this->write_buf = busw ? nand_write_buf16 : nand_write_buf; |
| 2379 | if (!this->read_buf) |
| 2380 | this->read_buf = busw ? nand_read_buf16 : nand_read_buf; |
| 2381 | if (!this->verify_buf) |
| 2382 | this->verify_buf = busw ? nand_verify_buf16 : nand_verify_buf; |
| 2383 | if (!this->scan_bbt) |
| 2384 | this->scan_bbt = nand_default_bbt; |
| 2385 | |
| 2386 | /* Select the device */ |
| 2387 | this->select_chip(mtd, 0); |
| 2388 | |
| 2389 | /* Send the command for reading device ID */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2390 | this->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2391 | |
| 2392 | /* Read manufacturer and device IDs */ |
| 2393 | nand_maf_id = this->read_byte(mtd); |
| 2394 | nand_dev_id = this->read_byte(mtd); |
| 2395 | |
| 2396 | /* Print and store flash device information */ |
| 2397 | for (i = 0; nand_flash_ids[i].name != NULL; i++) { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2398 | |
| 2399 | if (nand_dev_id != nand_flash_ids[i].id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | continue; |
| 2401 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2402 | if (!mtd->name) |
| 2403 | mtd->name = nand_flash_ids[i].name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | this->chipsize = nand_flash_ids[i].chipsize << 20; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | /* New devices have all the information in additional id bytes */ |
| 2407 | if (!nand_flash_ids[i].pagesize) { |
| 2408 | int extid; |
| 2409 | /* The 3rd id byte contains non relevant data ATM */ |
| 2410 | extid = this->read_byte(mtd); |
| 2411 | /* The 4th id byte is the important one */ |
| 2412 | extid = this->read_byte(mtd); |
| 2413 | /* Calc pagesize */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 2414 | mtd->writesize = 1024 << (extid & 0x3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | extid >>= 2; |
| 2416 | /* Calc oobsize */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 2417 | mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize >> 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | extid >>= 2; |
| 2419 | /* Calc blocksize. Blocksize is multiples of 64KiB */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2420 | mtd->erasesize = (64 * 1024) << (extid & 0x03); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | extid >>= 2; |
| 2422 | /* Get buswidth information */ |
| 2423 | busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2425 | } else { |
| 2426 | /* Old devices have this data hardcoded in the |
| 2427 | * device id table */ |
| 2428 | mtd->erasesize = nand_flash_ids[i].erasesize; |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 2429 | mtd->writesize = nand_flash_ids[i].pagesize; |
| 2430 | mtd->oobsize = mtd->writesize / 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2431 | busw = nand_flash_ids[i].options & NAND_BUSWIDTH_16; |
| 2432 | } |
| 2433 | |
Kyungmin Park | 0ea4a75 | 2005-02-16 09:39:39 +0000 | [diff] [blame] | 2434 | /* Try to identify manufacturer */ |
| 2435 | for (maf_id = 0; nand_manuf_ids[maf_id].id != 0x0; maf_id++) { |
| 2436 | if (nand_manuf_ids[maf_id].id == nand_maf_id) |
| 2437 | break; |
| 2438 | } |
| 2439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | /* Check, if buswidth is correct. Hardware drivers should set |
| 2441 | * this correct ! */ |
| 2442 | if (busw != (this->options & NAND_BUSWIDTH_16)) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2443 | printk(KERN_INFO "NAND device: Manufacturer ID:" |
| 2444 | " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id, |
| 2445 | nand_manuf_ids[maf_id].name, mtd->name); |
| 2446 | printk(KERN_WARNING |
| 2447 | "NAND bus width %d instead %d bit\n", |
| 2448 | (this->options & NAND_BUSWIDTH_16) ? 16 : 8, busw ? 16 : 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | this->select_chip(mtd, -1); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2450 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2452 | |
| 2453 | /* Calculate the address shift from the page size */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 2454 | this->page_shift = ffs(mtd->writesize) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | this->bbt_erase_shift = this->phys_erase_shift = ffs(mtd->erasesize) - 1; |
| 2456 | this->chip_shift = ffs(this->chipsize) - 1; |
| 2457 | |
| 2458 | /* Set the bad block position */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 2459 | this->badblockpos = mtd->writesize > 512 ? NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | |
| 2461 | /* Get chip options, preserve non chip based options */ |
| 2462 | this->options &= ~NAND_CHIPOPTIONS_MSK; |
| 2463 | this->options |= nand_flash_ids[i].options & NAND_CHIPOPTIONS_MSK; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2464 | /* Set this as a default. Board drivers can override it, if necessary */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2465 | this->options |= NAND_NO_AUTOINCR; |
| 2466 | /* Check if this is a not a samsung device. Do not clear the options |
| 2467 | * for chips which are not having an extended id. |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2468 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2469 | if (nand_maf_id != NAND_MFR_SAMSUNG && !nand_flash_ids[i].pagesize) |
| 2470 | this->options &= ~NAND_SAMSUNG_LP_OPTIONS; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | /* Check for AND chips with 4 page planes */ |
| 2473 | if (this->options & NAND_4PAGE_ARRAY) |
| 2474 | this->erase_cmd = multi_erase_cmd; |
| 2475 | else |
| 2476 | this->erase_cmd = single_erase_cmd; |
| 2477 | |
| 2478 | /* Do not replace user supplied command function ! */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 2479 | if (mtd->writesize > 512 && this->cmdfunc == nand_command) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2480 | this->cmdfunc = nand_command_lp; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2481 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2482 | printk(KERN_INFO "NAND device: Manufacturer ID:" |
| 2483 | " 0x%02x, Chip ID: 0x%02x (%s %s)\n", nand_maf_id, nand_dev_id, |
| 2484 | nand_manuf_ids[maf_id].name, nand_flash_ids[i].name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | break; |
| 2486 | } |
| 2487 | |
| 2488 | if (!nand_flash_ids[i].name) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2489 | printk(KERN_WARNING "No NAND device found!!!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | this->select_chip(mtd, -1); |
| 2491 | return 1; |
| 2492 | } |
| 2493 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2494 | for (i = 1; i < maxchips; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | this->select_chip(mtd, i); |
| 2496 | |
| 2497 | /* Send the command for reading device ID */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2498 | this->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2499 | |
| 2500 | /* Read manufacturer and device IDs */ |
| 2501 | if (nand_maf_id != this->read_byte(mtd) || |
| 2502 | nand_dev_id != this->read_byte(mtd)) |
| 2503 | break; |
| 2504 | } |
| 2505 | if (i > 1) |
| 2506 | printk(KERN_INFO "%d NAND chips detected\n", i); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2507 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2508 | /* Allocate buffers, if necessary */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | if (!this->oob_buf) { |
| 2510 | size_t len; |
| 2511 | len = mtd->oobsize << (this->phys_erase_shift - this->page_shift); |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2512 | this->oob_buf = kmalloc(len, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | if (!this->oob_buf) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2514 | printk(KERN_ERR "nand_scan(): Cannot allocate oob_buf\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2515 | return -ENOMEM; |
| 2516 | } |
| 2517 | this->options |= NAND_OOBBUF_ALLOC; |
| 2518 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | if (!this->data_buf) { |
| 2521 | size_t len; |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 2522 | len = mtd->writesize + mtd->oobsize; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2523 | this->data_buf = kmalloc(len, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2524 | if (!this->data_buf) { |
| 2525 | if (this->options & NAND_OOBBUF_ALLOC) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2526 | kfree(this->oob_buf); |
| 2527 | printk(KERN_ERR "nand_scan(): Cannot allocate data_buf\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | return -ENOMEM; |
| 2529 | } |
| 2530 | this->options |= NAND_DATABUF_ALLOC; |
| 2531 | } |
| 2532 | |
| 2533 | /* Store the number of chips and calc total size for mtd */ |
| 2534 | this->numchips = i; |
| 2535 | mtd->size = i * this->chipsize; |
| 2536 | /* Convert chipsize to number of pages per chip -1. */ |
| 2537 | this->pagemask = (this->chipsize >> this->page_shift) - 1; |
| 2538 | /* Preset the internal oob buffer */ |
| 2539 | memset(this->oob_buf, 0xff, mtd->oobsize << (this->phys_erase_shift - this->page_shift)); |
| 2540 | |
| 2541 | /* If no default placement scheme is given, select an |
| 2542 | * appropriate one */ |
| 2543 | if (!this->autooob) { |
| 2544 | /* Select the appropriate default oob placement scheme for |
| 2545 | * placement agnostic filesystems */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2546 | switch (mtd->oobsize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | case 8: |
| 2548 | this->autooob = &nand_oob_8; |
| 2549 | break; |
| 2550 | case 16: |
| 2551 | this->autooob = &nand_oob_16; |
| 2552 | break; |
| 2553 | case 64: |
| 2554 | this->autooob = &nand_oob_64; |
| 2555 | break; |
| 2556 | default: |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2557 | printk(KERN_WARNING "No oob scheme defined for oobsize %d\n", mtd->oobsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2558 | BUG(); |
| 2559 | } |
| 2560 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2562 | /* The number of bytes available for the filesystem to place fs dependend |
| 2563 | * oob data */ |
Thomas Gleixner | 998cf64 | 2005-04-01 08:21:48 +0100 | [diff] [blame] | 2564 | mtd->oobavail = 0; |
| 2565 | for (i = 0; this->autooob->oobfree[i][1]; i++) |
| 2566 | mtd->oobavail += this->autooob->oobfree[i][1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2568 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | * check ECC mode, default to software |
| 2570 | * if 3byte/512byte hardware ECC is selected and we have 256 byte pagesize |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2571 | * fallback to software ECC |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2572 | */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2573 | this->eccsize = 256; /* set default eccsize */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2574 | this->eccbytes = 3; |
| 2575 | |
| 2576 | switch (this->eccmode) { |
| 2577 | case NAND_ECC_HW12_2048: |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 2578 | if (mtd->writesize < 2048) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2579 | printk(KERN_WARNING "2048 byte HW ECC not possible on %d byte page size, fallback to SW ECC\n", |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 2580 | mtd->writesize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2581 | this->eccmode = NAND_ECC_SOFT; |
| 2582 | this->calculate_ecc = nand_calculate_ecc; |
| 2583 | this->correct_data = nand_correct_data; |
| 2584 | } else |
| 2585 | this->eccsize = 2048; |
| 2586 | break; |
| 2587 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2588 | case NAND_ECC_HW3_512: |
| 2589 | case NAND_ECC_HW6_512: |
| 2590 | case NAND_ECC_HW8_512: |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 2591 | if (mtd->writesize == 256) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2592 | printk(KERN_WARNING "512 byte HW ECC not possible on 256 Byte pagesize, fallback to SW ECC \n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2593 | this->eccmode = NAND_ECC_SOFT; |
| 2594 | this->calculate_ecc = nand_calculate_ecc; |
| 2595 | this->correct_data = nand_correct_data; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2596 | } else |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2597 | this->eccsize = 512; /* set eccsize to 512 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | break; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2599 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2600 | case NAND_ECC_HW3_256: |
| 2601 | break; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2602 | |
| 2603 | case NAND_ECC_NONE: |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2604 | printk(KERN_WARNING "NAND_ECC_NONE selected by board driver. This is not recommended !!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | this->eccmode = NAND_ECC_NONE; |
| 2606 | break; |
| 2607 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2608 | case NAND_ECC_SOFT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | this->calculate_ecc = nand_calculate_ecc; |
| 2610 | this->correct_data = nand_correct_data; |
| 2611 | break; |
| 2612 | |
| 2613 | default: |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2614 | printk(KERN_WARNING "Invalid NAND_ECC_MODE %d\n", this->eccmode); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2615 | BUG(); |
| 2616 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2617 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2618 | /* Check hardware ecc function availability and adjust number of ecc bytes per |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2619 | * calculation step |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2620 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | switch (this->eccmode) { |
| 2622 | case NAND_ECC_HW12_2048: |
| 2623 | this->eccbytes += 4; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2624 | case NAND_ECC_HW8_512: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | this->eccbytes += 2; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2626 | case NAND_ECC_HW6_512: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | this->eccbytes += 3; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2628 | case NAND_ECC_HW3_512: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | case NAND_ECC_HW3_256: |
| 2630 | if (this->calculate_ecc && this->correct_data && this->enable_hwecc) |
| 2631 | break; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2632 | printk(KERN_WARNING "No ECC functions supplied, Hardware ECC not possible\n"); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2633 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2636 | mtd->eccsize = this->eccsize; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2638 | /* Set the number of read / write steps for one page to ensure ECC generation */ |
| 2639 | switch (this->eccmode) { |
| 2640 | case NAND_ECC_HW12_2048: |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 2641 | this->eccsteps = mtd->writesize / 2048; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2642 | break; |
| 2643 | case NAND_ECC_HW3_512: |
| 2644 | case NAND_ECC_HW6_512: |
| 2645 | case NAND_ECC_HW8_512: |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 2646 | this->eccsteps = mtd->writesize / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | break; |
| 2648 | case NAND_ECC_HW3_256: |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2649 | case NAND_ECC_SOFT: |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 2650 | this->eccsteps = mtd->writesize / 256; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2651 | break; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2652 | |
| 2653 | case NAND_ECC_NONE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2654 | this->eccsteps = 1; |
| 2655 | break; |
| 2656 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2657 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2658 | /* Initialize state, waitqueue and spinlock */ |
| 2659 | this->state = FL_READY; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2660 | init_waitqueue_head(&this->wq); |
| 2661 | spin_lock_init(&this->chip_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2662 | |
| 2663 | /* De-select the device */ |
| 2664 | this->select_chip(mtd, -1); |
| 2665 | |
| 2666 | /* Invalidate the pagebuffer reference */ |
| 2667 | this->pagebuf = -1; |
| 2668 | |
| 2669 | /* Fill in remaining MTD driver data */ |
| 2670 | mtd->type = MTD_NANDFLASH; |
| 2671 | mtd->flags = MTD_CAP_NANDFLASH | MTD_ECC; |
| 2672 | mtd->ecctype = MTD_ECC_SW; |
| 2673 | mtd->erase = nand_erase; |
| 2674 | mtd->point = NULL; |
| 2675 | mtd->unpoint = NULL; |
| 2676 | mtd->read = nand_read; |
| 2677 | mtd->write = nand_write; |
| 2678 | mtd->read_ecc = nand_read_ecc; |
| 2679 | mtd->write_ecc = nand_write_ecc; |
| 2680 | mtd->read_oob = nand_read_oob; |
| 2681 | mtd->write_oob = nand_write_oob; |
| 2682 | mtd->readv = NULL; |
| 2683 | mtd->writev = nand_writev; |
| 2684 | mtd->writev_ecc = nand_writev_ecc; |
| 2685 | mtd->sync = nand_sync; |
| 2686 | mtd->lock = NULL; |
| 2687 | mtd->unlock = NULL; |
Vitaly Wool | 962034f | 2005-09-15 14:58:53 +0100 | [diff] [blame] | 2688 | mtd->suspend = nand_suspend; |
| 2689 | mtd->resume = nand_resume; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | mtd->block_isbad = nand_block_isbad; |
| 2691 | mtd->block_markbad = nand_block_markbad; |
| 2692 | |
| 2693 | /* and make the autooob the default one */ |
| 2694 | memcpy(&mtd->oobinfo, this->autooob, sizeof(mtd->oobinfo)); |
| 2695 | |
Thomas Gleixner | 0040bf3 | 2005-02-09 12:20:00 +0000 | [diff] [blame] | 2696 | /* Check, if we should skip the bad block table scan */ |
| 2697 | if (this->options & NAND_SKIP_BBTSCAN) |
| 2698 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | |
| 2700 | /* Build bad block table */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2701 | return this->scan_bbt(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | } |
| 2703 | |
| 2704 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2705 | * nand_release - [NAND Interface] Free resources held by the NAND device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2706 | * @mtd: MTD device structure |
| 2707 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2708 | void nand_release(struct mtd_info *mtd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2709 | { |
| 2710 | struct nand_chip *this = mtd->priv; |
| 2711 | |
| 2712 | #ifdef CONFIG_MTD_PARTITIONS |
| 2713 | /* Deregister partitions */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2714 | del_mtd_partitions(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2715 | #endif |
| 2716 | /* Deregister the device */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2717 | del_mtd_device(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2718 | |
Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 2719 | /* Free bad block table memory */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2720 | kfree(this->bbt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | /* Buffer allocated by nand_scan ? */ |
| 2722 | if (this->options & NAND_OOBBUF_ALLOC) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2723 | kfree(this->oob_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 | /* Buffer allocated by nand_scan ? */ |
| 2725 | if (this->options & NAND_DATABUF_ALLOC) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2726 | kfree(this->data_buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | } |
| 2728 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2729 | EXPORT_SYMBOL_GPL(nand_scan); |
| 2730 | EXPORT_SYMBOL_GPL(nand_release); |
Richard Purdie | 8fe833c | 2006-03-31 02:31:14 -0800 | [diff] [blame] | 2731 | |
| 2732 | static int __init nand_base_init(void) |
| 2733 | { |
| 2734 | led_trigger_register_simple("nand-disk", &nand_led_trigger); |
| 2735 | return 0; |
| 2736 | } |
| 2737 | |
| 2738 | static void __exit nand_base_exit(void) |
| 2739 | { |
| 2740 | led_trigger_unregister_simple(nand_led_trigger); |
| 2741 | } |
| 2742 | |
| 2743 | module_init(nand_base_init); |
| 2744 | module_exit(nand_base_exit); |
| 2745 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 2746 | MODULE_LICENSE("GPL"); |
| 2747 | MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com>, Thomas Gleixner <tglx@linutronix.de>"); |
| 2748 | MODULE_DESCRIPTION("Generic NAND flash driver code"); |