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