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