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