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